MathGroup Archive 2007

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

Search the Archive

Re: Cases to Select ?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73962] Re: [mg73920] Cases to Select ?
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Sat, 3 Mar 2007 03:56:06 -0500 (EST)
  • References: <200703030602.BAA02963@smc.vnet.net>

On 3 Mar 2007, at 07:02, 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}}
>
>  To retrieve those sublists containing 3 and 4, I'm
> using
>
>  A34C=Cases[A, {___, 4, ___, 3, ___} | {___, 3, ___,
> 4, ___}].
>
>  My first query is whether there is a (neater ?) way
> to combine the 2 patterns, possibly using MemberQ as
> in
>
>   A34S=Select[A, MemberQ[#, 3] && MemberQ[#, 4] &]  .

I am not sure why this is neater, but can certianly do the same thing  
with Cases:


Cases[A,_?(MemberQ[#,3]&&MemberQ[#,4]&)]


{{4,3,8},{3,7,4},{4,6,0,3},{-3,4,3,8,1}}

>
>
>  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.
>
>   Finally, how could I do it using Cases
>
>         NotA34C= Cases[A, pattern??]  ?


The simplest way is to use DeleteCases:

DeleteCases[A, _?(MemberQ[#, 3] && MemberQ[#, 4] &)]

Andrzej Kozlowski


  • Prev by Date: Re: Cases to Select ?
  • Next by Date: Re: nested list creation with index skipping
  • Previous by thread: Cases to Select ?
  • Next by thread: Re: Cases to Select ?