Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DeleteCases and multiple patterns

645 views
Skip to first unread message

guerom00

unread,
Nov 29, 2008, 4:29:11 AM11/29/08
to
Hello everyone :)

I have, say a list of list. Each element is a list of length 3 i.e.

myList={{x1,y1,z1},{x2,y2,z2},...{xN,yN,zN}}

Now, let's say I want to delete from this list the element which have
x=2 and 3 using DeleteCases.
This works :

DeleteCase[myList,{2,_,_}]
DeleteCases[myList,{3,_,_}]

but I would like to use

DeleteCases[myList,{{2,_,_},{3,_,_}}]

and this does not work...

How do I combine multiple patterns in DeleteCases ?

Thanks in advance.

m.g.

unread,
Nov 30, 2008, 6:57:06 AM11/30/08
to

Hello guero,

I do not know how to realize this with "DeleteCases", but one
solution ist "the other way round" to select the elements you want
(and not to delete the ones you do not want): eg:

Select[myList,!((First[#] == 2 || First[#] == 3) &)]

this should work

Greetings

Mike

Bill Rowe

unread,
Nov 30, 2008, 6:58:07 AM11/30/08
to
On 11/29/08 at 4:29 AM, guer...@gmail.com (guerom00) wrote:

>I have, say a list of list. Each element is a list of length 3 i.e.

>myList={{x1,y1,z1},{x2,y2,z2},...{xN,yN,zN}}

>Now, let's say I want to delete from this list the element which
>have x=2 and 3 using DeleteCases. This works :

>DeleteCase[myList,{2,_,_}] DeleteCases[myList,{3,_,_}]

>but I would like to use

>DeleteCases[myList,{{2,_,_},{3,_,_}}]

>and this does not work...

>How do I combine multiple patterns in DeleteCases ?

Rather than trying to use multiple patterns, try using a single
pattern using Alternatives. That is

DeleteCases[myList, {Aternatives@@{2,3},__}]

or

DeleteCases[myList, {2 | 3,__}]

will do what you want

Albert Retey

unread,
Nov 30, 2008, 6:57:43 AM11/30/08
to
Hi,

I think you can't, but there is no need to do that, since you can
formulate patterns that match more than one case with Alternatives,
or its short form | . So this should do what you want:

DeleteCases[myList,{2,_,_}|{3,_,_}]

or, in this case:

DeleteCases[myList,{2|3,_,_}]

hth,

albert


Bob Hanlon

unread,
Nov 30, 2008, 6:58:30 AM11/30/08
to
DeleteCases[myList, {2 | 3, __}]


Bob Hanlon

---- guerom00 <guer...@gmail.com> wrote:

=============
Hello everyone :)

I have, say a list of list. Each element is a list of length 3 i.e.

myList={{x1,y1,z1},{x2,y2,z2},...{xN,yN,zN}}

Now, let's say I want to delete from this list the element which have
x=2 and 3 using DeleteCases.
This works :

DeleteCase[myList,{2,_,_}]
DeleteCases[myList,{3,_,_}]

but I would like to use

DeleteCases[myList,{{2,_,_},{3,_,_}}]

and this does not work...

How do I combine multiple patterns in DeleteCases ?

Thanks in advance.


--

Bob Hanlon


Raffy

unread,
Dec 1, 2008, 6:59:54 AM12/1/08
to

DeleteCases[ myList, {2,_,_}|{3,_,_} ]
DeleteCases[ myList, {2|3, _, _} ]

However, if your search only involves the first element, the following
would be faster:

Pick[ myList, myList[[All, 1]], Except[2 | 3, _Integer]]

dimitris

unread,
Dec 1, 2008, 7:01:41 AM12/1/08
to
On 29 =CD=EF=DD, 11:29, guerom00 <guero...@gmail.com> wrote:
> Hello everyone :)
>
> I have, say a list of list. Each element is a list of length 3 i.e.
>
> myList={{x1,y1,z1},{x2,y2,z2},...{xN,yN,zN}}
>
> Now, let's say I want to delete from this list the element which have
> x=2 and 3 using DeleteCases.
> This works :
>
> DeleteCase[myList,{2,_,_}]
> DeleteCases[myList,{3,_,_}]
>
> but I would like to use
>
> DeleteCases[myList,{{2,_,_},{3,_,_}}]
>
> and this does not work...
>
> How do I combine multiple patterns in DeleteCases ?
>
> Thanks in advance.

Hi,

In[19]:=
mylist = Table[Random[Integer, {0, 5}], {10}, {3}]

Out[19]=
{{1, 2, 1}, {3, 5, 3}, {2, 5, 1}, {0, 5, 0}, {3, 2, 0}, {4, 1, 0}, {1,
3, 5}, {0, 0, 2}, {0, 3, 4}, {4, 1, 4}}

In[20]:=
DeleteCases[mylist, {2, _, _} | {3, _, _} | {0, _, _}]

Out[20]=
{{1, 2, 1}, {4, 1, 0}, {1, 3, 5}, {4, 1, 4}}

In[15]:=
Names["|"]

Out[15]=
{"Alternatives"}

Dimitris

PS

See
http://verbeia.com/mathematica/tips/Tricks.html

for a supplementery help browser from a mathematica guru
regarding such functions.

Regards
Dimitris

guerom00

unread,
Dec 2, 2008, 12:28:34 AM12/2/08
to
Ah=85
Thanks everyone, I had try || (=93or=94) but not this alternatives
thing :)

Thanks

0 new messages