3

If I have a list of lists with different lengths:

{{1, 2, 3}, {1}, {1, 2, 3, 4}, {x}, {1, x}, {2}, {2, 3}}

How can I extract all the sub-lists with no symbols:

{{1, 2, 3}, {1}, {1, 2, 3, 4}, {2}, {2, 3}}

There must be an easy way to do this but I can't find it. Unfortunately, I'm not so good with Mathematica to be able to modify similar cases that I found(for example Link1 and Link2) to the problem at hand.

MathX
  • 1,614
  • 11
  • 17

3 Answers3

6
Cases[{{1, 2, 3}, {1}, {1, 2, 3, 4}, {x}, {1, x}, {2}, {2, 3}}, {__?NumericQ}]
march
  • 23,399
  • 2
  • 44
  • 100
5

Another approach is to use VectorQ:

lis = {{1, 2, 3}, {1}, {1, 2, 3, 4}, {x}, {1, x}, {2}, {2, 3}};
Select[lis, VectorQ[#, NumericQ] &]

(* {{1, 2, 3}, {1}, {1, 2, 3, 4}, {2}, {2, 3}} *)

Variations:

Select[lis, AllTrue[#, NumericQ] &]
RunnyKine
  • 33,088
  • 3
  • 109
  • 176
4

I think @march's answer is probably the best way. Here's another for giggles...

Select[{{1, 2, 3}, {1}, {1, 2, 3, 4}, {x}, {1, x}, {2}, {2, 3}}, ! MemberQ[#, _Symbol] &]
kale
  • 10,922
  • 1
  • 32
  • 69
  • Interesting. What is the name of that _Symbol part? I want to search for other properties that have the same usage. – MathX Mar 17 '16 at 23:24
  • 2
    @BehzadNazari That's shortcut for Blank[Symbol] which is a pattern match for a certain Head. Head[x] (if x isn't defined) evaluates to Symbol. Try Head /@ {x, 1.0, 1, "test"} for examples. – kale Mar 18 '16 at 02:10