MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Cases to Select ?


Mr Ajit Sen wrote:
>  Dear MathGroup,
> 
>  Consider the following list:
> 
>   A={{1, 4, 5}, {3, -6, 1}, { 2, 0, 4}, {4, 3, 8},
> {-5, 1, 4}, {3, 7, 4},{4,6,0,3}, {-3, 4, 3, 8, 1}}

[snip]

>  Now, say I'd like to pick out those sublists that do
> not contain 3 and 4.  Then I don't get the result
> {3,-6,1} with
> 
>    NotA34S=Select[A, (FreeQ[#, 3] && FreeQ[#, 4]) &]
> 
>  nor with 
>           Select[A, (FreeQ[#, 3] || FreeQ[#, 4]) &]
>  nor with
>           Complement[A,A34S]
> 
>  Why not?  I don't think !MemberQ exists. Looking at
> all negations in the Help browser, I guess that Unsame
> could do the trick but am unable to apply it.

In your original list, every sublist contains either a 3 or a 4 or both.

In[1]:=
A = {{1, 4, 5}, {3, -6, 1}, {2, 0, 4}, {4, 3, 8},
     {-5, 1, 4}, {3, 7, 4}, {4, 6, 0, 3},
     {-3, 4, 3, 8, 1}};

In[2]:=
NotA34S = Select[A, FreeQ[#1, 3] && FreeQ[#1, 4] & ]

Out[2]=
{}

In[3]:=
NotA34S = Select[A, FreeQ[#1, 3] || FreeQ[#1, 4] & ]

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

Now let's try with a list that contains at least one sublist without 3 
and 4,

In[4]:=
A = {{1, 4, 5}, {3, -6, 1}, {2, 0, 4}, {4, 3, 8},
     {-5, 1, 4}, {3, 7, 4}, {4, 6, 0, 3},
     {-3, 4, 3, 8, 1}, {-3, -4}};

In[5]:=
NotA34S = Select[A, FreeQ[#1, 3] && FreeQ[#1, 4] & ]

Out[5]=
{{-3, -4}}

In[6]:=
NotA34S = Select[A, FreeQ[#1, 3] || FreeQ[#1, 4] & ]

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

>   Finally, how could I do it using Cases
> 
>         NotA34C= Cases[A, pattern??]  ?
> 

You could try something along the line

In[7]:=
NotA34C = Cases[A, {___, x_, ___} /; x == 3 || x == 4]

Out[7]=
{{1, 4, 5}, {3, -6, 1}, {2, 0, 4}, {4, 3, 8},
   {-5, 1, 4}, {3, 7, 4}, {4, 6, 0, 3},
   {-3, 4, 3, 8, 1}}

In[8]:=
NotA34C = DeleteCases[A, {___, x_, ___} /;
     x == 3 || x == 4]

Out[8]=
{{-3, -4}}

HTH,
Jean-Marc


  • Prev by Date: Cases to Select : Erratum [Wrong Sublist]
  • Next by Date: Re: Cases to Select ?
  • Previous by thread: Re: Cases to Select ?
  • Next by thread: nested list creation with index skipping