Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 6 down vote accepted
Cases[{{1, 2, 3}, {1}, {1, 2, 3, 4}, {x}, {1, x}, {2}, {2, 3}}, {__?NumericQ}]
share|improve this answer
4  
Or 3 underscores if empty lists should also be kept. – Martin Büttner Mar 17 at 21:42
    
Perfect! Thank you. – MathX Mar 17 at 21:44
    
Or DeleteCases[ {{1, 2, 3}, {1}, {1, 2, 3, 4}, {x}, {1, x}, {2}, {2, 3}}, {___, _Symbol, ___}] – Shutao TANG Mar 18 at 1:58

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] &]
share|improve this answer

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] &]
share|improve this answer
    
Interesting. What is the name of that _Symbol part? I want to search for other properties that have the same usage. – MathX Mar 17 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 at 2:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.