5
$\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
    $\begingroup$ Cases[{__?NumericQ}] @ ll $\endgroup$ – Kuba Feb 16 at 9:44
  • 1
    $\begingroup$ @Kuba Thanks for this nice solution! $\endgroup$ – Ulrich Neumann Feb 16 at 9:51
5
$\begingroup$

This should work:

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

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

|improve this answer|||||
$\endgroup$
  • 2
    $\begingroup$ Thanks for your answer, I didn't know AllTrue $\endgroup$ – Ulrich Neumann Feb 16 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 at 10:30
  • $\begingroup$ Also: Select[AllTrue@NumericQ]@ll $\endgroup$ – eldo Feb 16 at 20:08

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.