6
$\begingroup$

I got stuck with a simple problem:

Given a list of lists

ll={{1,2,3},{2,a},{Pi,4,5}}

I would like to select only those sublists which are numeric -> {{1,2,3}, {Pi,4,5}}.

I tried

Select[ll, (Map[Apply[And, NumericQ], #] &)] 

which doesn't work.

How to solve this problem? Thanks!

$\endgroup$
2
  • 2
    $\begingroup$ Cases[{__?NumericQ}] @ ll $\endgroup$
    – Kuba
    Feb 16, 2020 at 9:44
  • 1
    $\begingroup$ @Kuba Thanks for this nice solution! $\endgroup$ Feb 16, 2020 at 9:51

1 Answer 1

5
$\begingroup$

This should work:

Select[ll, AllTrue[#, NumericQ] &]

{{1, 2, 3}, {[Pi], 4, 5}}

$\endgroup$
3
  • 2
    $\begingroup$ Thanks for your answer, I didn't know AllTrue $\endgroup$ Feb 16, 2020 at 9:52
  • 1
    $\begingroup$ @UlrichNeumann neither did I xD I was somehow convinced that All was a mathematica function for doing something like that (while it's actually all() and is a python function eheh), but while looking for it I found AllTrue in the documentation :P $\endgroup$
    – Fraccalo
    Feb 16, 2020 at 10:30
  • 1
    $\begingroup$ Also: Select[AllTrue@NumericQ]@ll $\endgroup$
    – eldo
    Feb 16, 2020 at 20:08

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.