Re: How to do a negative pattern?
- To: mathgroup at smc.vnet.net
- Subject: [mg90579] Re: How to do a negative pattern?
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Mon, 14 Jul 2008 05:22:09 -0400 (EDT)
On 7/13/08 at 3:47 PM, stevensiew2 at gmail.com (Steven Siew) wrote:
>I have used the function DeleteCases which has the format
>DeleteCases[expr,pattern]
>However what I wanted is a negative pattern like
>DeleteCases[expr,negpattern]
>But I have no idea how to craft a negative pattern. Alternatively I
>suppose I could do with a DeleteCasesNot function. Something like
>this:
>In[3]:= mylist={ {1,1},{1,2},{1,3},{2,1},{2,2},{2,3} }
>In[5]:= myselectionB = DeleteCasesNot[mylist,{_,3}]
>Out[5]= {{1, 3}, {2, 3}}
>Does anyone has any idea how I could code up a DeleteCasesNot
>function?
You can achieve what you want using DeleteCases with the
appropriate pattern, i.e.,
In[2]:= DeleteCases[mylist, {_, _?(# != 3 &)}]
Out[2]= {{1, 3}, {2, 3}}
But note deleting all cases that do not match a given pattern is
the same as keeping all cases that do match the pattern. So, the
same result can be achieved with
In[3]:= Cases[mylist, {_, 3}]
Out[3]= {{1, 3}, {2, 3}}