Suppose I have a nested list such as,
{{{A, B}, {A, D}}, {{C, D}, {A, A}, {H, A}}, {{A, H}}}
Where the elements of interest are,
{{A, B}, {A, D}}
{{C, D}, {A, A}, {H, A}}
{{A, H}}
How would I use select to pick up only elements that contain two or more As in the first part of their sub-elements. In this example I would want the following as an output,
{{A,B},{A,D}}
Answer
list = {{{a, b}, {a, d}}, {{c, d}, {a, a}, {h, a}}, {{a, h}}}
Pick[list, Count[#[[All, 1]], a] >= 2 & /@ list]
or
Select[list, Count[#[[All, 1]], a] >= 2 &]
or
Cases[list, _?(Count[#[[All, 1]], a] >= 2 &)]
or
DeleteCases[list, _?(! Count[#[[All, 1]], a] >= 2 &)]
all give
{{{a, b}, {a, d}}}
Comments
Post a Comment