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.
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
>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
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
---- 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
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]]
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
Thanks