2
$\begingroup$

I was trying to make this

L1 = {{2, 1}, {6, 4}, {8, 7}, {9, 5}};
L2 = {{2, 9}, {9, 6}};
L = Union[L2 , L1]

And I used this:

Cases[L, {{a_, _}, {b_, _}} /; a == b]
Complement[L,L2]

And this:

DeleteCases[L, MemberQ[L1, {#[[1]], _}] & /@ L2]

But, both fail. The answer must take from L1 only the elements where the first element appear in L2. For L1 and L2 above, must return

{{2,1},{9,5}} 

And {6, 4} and {8, 7} must be dropped from L2!

Thanks,

$\endgroup$
5
$\begingroup$

Using Cases:

L1 = {{2, 1}, {6, 4}, {8, 7}, {9, 5}};
L2 = {{2, 9}, {9, 6}};

With[{a = Alternatives @@ L2[[All, 1]]}, Cases[L1, {a, _}]]
$\endgroup$
  • $\begingroup$ And the complement to your answer: With[{a = Alternatives @@ L2[[All, 1]]}, DeleteCases[L1, {Except@a, _}]] :) $\endgroup$ – rm -rf Dec 17 '13 at 20:40
  • $\begingroup$ Simon, why use With here? Clarity? $\endgroup$ – Mr.Wizard Dec 18 '13 at 0:59
  • $\begingroup$ @Mr.Wizard, yes, just for clarity. Not really necessary here, but I did some work recently with rather complex string patterns and got into the habit of doing this to make StringCases and StringMatchQ expressions more readable. $\endgroup$ – Simon Woods Dec 18 '13 at 9:46
  • $\begingroup$ Okay, just checking. I sometimes use Function the same way, e.g. Alternatives @@ L2[[All, 1]] // Cases[L1, {#, _}] &. $\endgroup$ – Mr.Wizard Dec 18 '13 at 9:49
  • $\begingroup$ Could you tell me if I am wrong: the command With will take the cases in L1 where a could be any of the first elements in the list L2 (2 and 9 for this example)...it is ok? $\endgroup$ – Jotasmall Dec 18 '13 at 17:34
2
$\begingroup$
L1 = {{2, 1}, {6, 4}, {8, 7}, {9, 5}};
L2 = {{2, 9}, {9, 6}};
Select[L1, MemberQ[L2[[All, 1]], First@#] &]
(*
{{2, 1}, {9, 5}}
*)
$\endgroup$
  • $\begingroup$ This will reevaluate L2[[All, 1]] for every element of L1. This is not ideal for long lists. $\endgroup$ – Mr.Wizard Dec 18 '13 at 1:01
  • $\begingroup$ @Mr.Wizard ` When deciding whether to optimize a specific part of the program, Amdahl's Law should always be considered: the impact on the overall program depends very much on how much time is actually spent in that specific part, which is not always clear from looking at the code without a performance analysis.` $\endgroup$ – Dr. belisarius Dec 18 '13 at 1:37

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.