3
$\begingroup$

I am new to MMA. I have a question of joining two lists according to a condition. Suppose I have two lists with same number of elements,

l1={a>b,x>y,m>n}
l2={c>d,False,j>k}

I want to join the two lists according to a condition: join each list's element according to their positions in the list if they both are inequalities. If there is a False in l2, then ignore both elements of that position. For example,

  1. The first element of l1 is a>b, and the first element of l2 is c>d, then the first element of the new list will be {a>b,c>d}.
  2. The second element of l1 is x>y, but the second element of l2 is False. So we will ignore position number 2.
  3. The third element of l1 is m>n, and the third element of l2 is j>k, then the second element of the new list will be{m>n,j>k}.

Therefore, the new list will be {{a>b,c>d},{m>n,j>k}}.

Another example:

l3={a>b,c>d,x>y,j>k}
l4={False,False,False,m>n}.

Then the new list I want will be {{j>k,m>m}}

Thank you in advance for your generous help!

$\endgroup$
1
  • 1
    $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Jul 24 at 3:56
3
$\begingroup$

One way of going about it is with a, in some sense, fundamental part of MMA called pattern matching via DeleteCases.

l1 = {a > b, x > y, m > n};
l2 = {c > d, False, j > k};
Transpose@{l1, l2} (*Gets corresponding pairs of elements*)
DeleteCases[
  % (*This is the shorthand to refer to the previous output and is technically Out[-1]*),
  {_, False} (*The general structure of a pair of elements such that the second one is exactly False*)
]
(*Output 1*) {{a > b, c > d}, {x > y, False}, {m > n, j > k}}
(*Output 2*) {{a > b, c > d}, {m > n, j > k}}

Here we first transposed the two lists of vectors so that we get a lists of the corresponding elements in each list. Then it's a matter of telling Mathematica which "forms" or "cases" to look out for and delete them with the fortunately native function DeleteCases.

I have a feeling there's another way to do it with Pick, but also that it would take some finagling and, while faster, be more of a "because you can" method.

$\endgroup$
4
$\begingroup$
l1 = {a > b, x > y, m > n};
l2 = {c > d, False, j > k};
MapThread[
 If[#1 =!= False && #2 =!= False, List[#1, #2], Nothing] &, {l1, l2}]

{{a > b, c > d}, {m > n, j > k}}

$\endgroup$
4
$\begingroup$
Select[Transpose[{l1,l2}],#[[2]] =!= False &]

(* {{a > b, c > d}, {m > n, j > k}} *)


Select[Transpose[{l3,l4}],#[[2]] =!= False &]

(* {{j > k, m > n}} *) 

where

l1={a>b,x>y,m>n}
l2={c>d,False,j>k}
l3={a>b,c>d,x>y,j>k}
l4={False,False,False,m>n}

(Original Answer)

Select[Transpose[{l1,l2}],FreeQ[#[[2]], False]&]
(* {{a > b, c > d}, {m > n, j > k}} *)
$\endgroup$
1
  • 1
    $\begingroup$ Or, using the operator form of Select: Transpose[{l1,l2}]//Select[#[[2]] =!= False &] $\endgroup$
    – user1066
    Jul 24 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.