I have a problem that, admittedly, I have already solved using Select
instead, but it is irking me that I cannot seem to construct the right pattern to solve it using Cases
. I would like the output of
Cases[
{{{1, 2}, {3}, {4, 5}}, {{6}, {}}, {{}, {}}},
(* THE CORRECT PATTERN HERE *)
]
to be
{{{1, 2}, {3}, {4, 5}}, {{6}, {}}}
In other words, I would like a pattern that picks up all the elements of the outer list that have at least one non-empty list as an element.
Thanks in advance for your help.
Answer
list = {{{1, 2}, {3}, {4, 5}}, {{6}, {}}, {{}, {}}};
Cases[list, Except[{{} ..}]]
(* {{{1, 2}, {3}, {4, 5}}, {{6}, {}}} *)
or
Cases[list, {___, Except[{}], ___}]
(* {{{1, 2}, {3}, {4, 5}}, {{6}, {}}} *)
You can also use PatternTest
(_?func
) where func
is any selector function that you might have used as the second argument of Select
. For example:
Select[list, Union @@ # =!= {} &] (* or Flatten @ # =!= {} & or ... *)
(* {{{1, 2}, {3}, {4, 5}}, {{6}, {}}} *)
Cases[list, _?(Union @@ # =!= {} &)]
(* {{{1, 2}, {3}, {4, 5}}, {{6}, {}}} *)
Comments
Post a Comment