Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Consider the following:

list={1/First[{}], 1, 2, 1/First[{}], 3};
DeleteCases[list,_NumberQ]

I wanted to remove all cases, which did not match _!NumberQ (e.g. 1/First[{}]), in the first place. But after DeleteCases[list,_!NumberQ] did not work, I tried it with DeleteCases[list,_NumberQ], just to see whether that would work...didn't.

What I am doing wrong?

share|improve this question
2  
NumberQ is a predicate function so to make a pattern you need _?NumberQ –  Andrzej Kozlowski Mar 25 '12 at 17:35
    
just curious... is there a reason you unaccepted mine? –  rm -rf Mar 25 '12 at 17:56
    
R.M., sorry, I thought I did. Sorry! –  John Mar 25 '12 at 19:59
1  
@John Have you considered if one of the answers can be accepted ? I would suggest to accept R.M.'s answer, but you can do whatever you would like and I think the both answers give good solutions to the problem. –  Artes Apr 29 '12 at 21:17

2 Answers 2

up vote 13 down vote accepted

The syntax _foo indicates that you're looking for a pattern with the head foo. NumberQ is not a Head, but a test returning a boolean True or False depending on whether the expression is a number or not. So you'd have to use it with PatternTest as _?NumberQ. For your example, the following should work:

Cases[list, _?NumberQ]

If you wanted to stick with DeleteCases, then you'll have to negate the test using either of the three constructs below:

DeleteCases[list, _?(Composition[Not, NumberQ])]
DeleteCases[list, _?(! NumberQ[#] &)]
DeleteCases[list, Except[_?NumberQ]]

Beware that ? has a very high precedence and binds very tightly and hence the parentheses are necessary in the first two constructs. See this question for more info.

share|improve this answer
    
Here another DeleteCases-issue: {{"Bla1","Bla2","Bla3","Bla4"}, {-0.81682, 0.373522}, {0.373522, -0.451794}} DeleteCases[%, {# > 0, # < 0} &] I fear, that I still have a great lack of basic knowledge... :-/ –  John Mar 25 '12 at 19:59
    
@John This example better fits to use of Select, look at my answer. –  Artes Mar 25 '12 at 20:26
    
@John ...curious... is there a reason you unaccepted my answer? –  Artes Mar 25 '12 at 20:27
    
@Artes: This is really odd. I swear I didn't unaccepted it. Sorry, though. –  John Mar 25 '12 at 20:52
1  
@John I think you're confused here. You can "accept" only one answer, not both. When you check more than one answer, the checkmark gets removed from the previous answer, thereby "unaccepting" it. You can see this in the timeline, where you first accepted mine, then Artes', then mine again and back to Artes'. Generally, people accept the answer that is clear and was helpful to them. In cases of competing answers, different users have different ways to decide whose to pick — votes/clarity/elegance/coin-toss... You gotta pick yours :) –  rm -rf Mar 25 '12 at 21:43

Consider the following :

Cases[list, _?NumberQ]

or

DeleteCases[list, Except[_?NumberQ]]

you could also use :

DeleteCases[list, Except[_?NumericQ]]

Edit

The reason one misuses DeleteCases is because there are similar constructs which work like this (giving the same result) :

Select[list, NumberQ]

i.e. Select[list, criterion] picks out elements of the list for which criterion is True unlike DeleteCases[list, pattern] which removes elements of list that match given a pattern. DeleteCases is supposed to work with expressions, while Select works basically with lists, although list can have any head, not only List.

NumberQ is a more restictive function than NumericQ, since the latter gives True also for symbols like e.g. E :

Head /@ {E, N[E]}

{Symbol, Real}

{NumberQ[E], NumericQ[E]}

{False, True}

share|improve this answer
1  
NumberQ and NumericQ are not the same. For example: Through[{NumberQ, NumericQ}@π] –  rm -rf Mar 25 '12 at 17:55
    
NumberQ and NumericQ do the same work for this kind of list, however they may yield different results in general. –  Artes Mar 25 '12 at 18:03
    
I never said you should remove your answer :) I merely observed that identical solutions were posted merely 2 seconds apart. I've never had such a close call, although I've seen closer –  rm -rf Mar 25 '12 at 18:06
    
I don't find your answers unhelpful at all! I think I've voted for most of them. –  rm -rf Mar 26 '12 at 0:54
    
@R.M Thank You ! Nevertheless I see I could improve most of my answers but in general I try to avoid to many edits. On the other hand I learn mainly when I try to give answers, even though I don't have them right now. –  Artes Mar 26 '12 at 1:06

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.