Re: How to do a negative pattern?
- To: mathgroup at smc.vnet.net
- Subject: [mg90573] Re: How to do a negative pattern?
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Mon, 14 Jul 2008 05:21:01 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <g5dm6i$ht4$1@smc.vnet.net> <487AD9D7.8090209@gmail.com>
Jean-Marc Gulliet wrote:
> 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:
>>
>> $ cat file012.out
>> Mathematica 5.2 for Students: Microsoft Windows Version
>> Copyright 1988-2005 Wolfram Research, Inc.
>>
>> In[1]:=
>> Out[1]= {stdout}
>>
>> In[2]:= (* Write your mathematica code below *)
>>
>> In[3]:= mylist={ {1,1},{1,2},{1,3},{2,1},{2,2},{2,3} }
>>
>> Out[3]= {{1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}}
>>
>> In[4]:= myselectionA = DeleteCases[mylist,{_,1}]
>>
>> Out[4]= {{1, 2}, {1, 3}, {2, 2}, {2, 3}}
>>
>> In[5]:= myselectionB = DeleteCasesNot[mylist,{_,3}]
>>
>> Out[5]= {{1, 3}, {2, 3}}
>>
>> In[6]:= (* End of mathematica code *)
>>
>> In[7]:= Quit[];
>>
>>
>> Does anyone has any idea how I could code up a DeleteCasesNot
>> function?
>
> I do not have the slightest idea about what a "negative" pattern
> can possibly be (at least in the context of Mathematica programming).
> It seems that you are looking for *Cases[]*, however.
>
> lst = {{1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}};
>
> Cases[lst, {_, 3}]
>
> DeleteCases[lst, {_, 3}]
>
>
> {{1, 3}, {2, 3}}
>
> {{1, 1}, {1, 2}, {2, 1}, {2, 2}}
Or, perhaps, the following will match more closely what you had in mind
when talking about "negative" patterns:
lst = {{1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}};
DeleteCases[lst, {_, Except[3]}]
DeleteCases[lst, {_, _?(# != 3 &)}]
DeleteCases[lst, {_, x_ /; x != 3}]
{{1, 3}, {2, 3}}
{{1, 3}, {2, 3}}
{{1, 3}, {2, 3}}
Regards,
-- Jean-Marc