4
$\begingroup$

I have:

aa = {{1, 1}, {2, 2}}

bb = {{4, 4}, {5, 5}}

cc = {{a, {1, 1}}, {b, {2, 2}}, {c, {3, 3}}, {d, {4, 4}}, {e, {5, 5}}, {f, {6, 6}}}

How can I select all elements form cc that contain all elements from aa as well as from bb?

The result should be:

{{a, {1, 1}}, {b, {2, 2}}, {d, {4, 4}}, {e, {5, 5}}}
$\endgroup$
2
  • 5
    $\begingroup$ ... if you've already assigned values to a and b, they will not show up in c. $\endgroup$ Commented Sep 13, 2017 at 16:34
  • $\begingroup$ sorry for the mistake ... $\endgroup$
    – mrz
    Commented Sep 13, 2017 at 18:11

8 Answers 8

5
$\begingroup$

You may use ContainsAny.

Select[cc, ContainsAny[Join[aa, bb]]]
{{a, {1, 1}}, {b, {2, 2}}, {d, {4, 4}}, {e, {5, 5}}}

Hope this helps.

$\endgroup$
5
$\begingroup$

Noting J.M.'s observation in the comments, I redefined your lists as

aa = {{1, 1}, {2, 2}};
bb = {{4, 4}, {5, 5}};
cc = {{a, {1, 1}}, {b, {2, 2}}, {c, {3, 3}}, {d, {4, 4}}, {e, {5, 5}}, {f, {6, 6}}};

Then,

Cases[cc, {_, Alternatives @@ Join[aa, bb]}, {1}]
(* {{a, {1, 1}}, {b, {2, 2}}, {d, {4, 4}}, {e, {5, 5}}} *)
$\endgroup$
4
$\begingroup$

Several methods in addition to Cases in @march's answer:

Select[cc, MemberQ[Join[aa, bb], #[[2]]] &]
Pick[cc,  MemberQ[Join[aa, bb], #[[2]]] & /@ cc]
DeleteCases[cc, _?(! MemberQ[Join[aa, bb], #[[2]]] &)]

{{a, {1, 1}}, {b, {2, 2}}, {d, {4, 4}}, {e, {5, 5}}}

$\endgroup$
3
$\begingroup$
Select[cc, MemberQ[Join @@ Thread[aa | bb]]]
{{a, {1, 1}}, {b, {2, 2}}, {d, {4, 4}}, {e, {5, 5}}}
$\endgroup$
4
  • $\begingroup$ Also ContainsAny[Join[aa, bb]]. $\endgroup$
    – Edmund
    Commented Sep 13, 2017 at 23:31
  • $\begingroup$ @Edmund Nice, but unfortunately not available in version 10.1 which I use. $\endgroup$
    – Mr.Wizard
    Commented Sep 13, 2017 at 23:39
  • 2
    $\begingroup$ Can't you offer WRI some rep for an upgrade? $\endgroup$
    – Edmund
    Commented Sep 13, 2017 at 23:41
  • $\begingroup$ @Edmund: Please add your ContainsAny-Solution as an answer ... I like it very much. $\endgroup$
    – mrz
    Commented Sep 14, 2017 at 7:41
2
$\begingroup$

It is also possible by using rules:

cc /. {s_Symbol, pair_List} /; !MemberQ[Join[aa, bb], pair] -> Nothing
$\endgroup$
1
$\begingroup$
aa = {{1, 1}, {2, 2}};

bb = {{4, 4}, {5, 5}};

cc = 
 {{a, {1, 1}}, {b, {2, 2}}, {c, {3, 3}}, {d, {4, 4}}, 
  {e, {5, 5}}, {f, {6, 6}}};

Using SequenceSplit (new in 11.3)

Catenate @ SequenceSplit[cc, {x_} /; ContainsNone[x, Join[aa, bb]]]

{{a, {1, 1}}, {b, {2, 2}}, {d, {4, 4}}, {e, {5, 5}}}

$\endgroup$
1
$\begingroup$

Using IntersectingQ:

aa = {{1, 1}, {2, 2}};
bb = {{4, 4}, {5, 5}};
cc = {{a, {1, 1}}, {b, {2, 2}}, {c, {3, 3}}, {d, {4, 4}}, {e, {5, 
    5}}, {f, {6, 6}}};

Select[cc, IntersectingQ[{Last@#}, Join[aa, bb]] &]

Result:

{{a, {1, 1}}, {b, {2, 2}}, {d, {4, 4}}, {e, {5, 5}}}

$\endgroup$
1
$\begingroup$
aa = {{1, 1}, {2, 2}};

bb = {{4, 4}, {5, 5}};

cc = {{a, {1, 1}}, {b, {2, 2}}, {c, {3, 3}}, {d, {4, 4}}, {e, {5, 5}}, {f, {6, 6}}};

Using MapAt:

MapAt[Nothing, cc, 
Position[cc, x_ /; FreeQ[x, Alternatives @@ Join[aa, bb]], 1, Heads -> False]]

{{a, {1, 1}}, {b, {2, 2}}, {d, {4, 4}}, {e, {5, 5}}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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