Re: Select from list
- To: mathgroup at smc.vnet.net
- Subject: [mg87780] Re: [mg87729] Select from list
- From: Curtis Osterhoudt <cfo at lanl.gov>
- Date: Wed, 16 Apr 2008 22:32:41 -0400 (EDT)
- Organization: LANL
- References: <200804160904.FAA23961@smc.vnet.net>
- Reply-to: cfo at lanl.gov
One possible way:
In[46]:= Flatten@
Position[(MemberQ[#, 2] && MemberQ[#, 3] &) /@ ptX, True]
Out[46]= {1, 2, 5}
On Wednesday 16 April 2008 03:04:08 Steve Gray wrote:
> I have a list like this:
>
> ptX=
> {{1, 2, 3, 4}, {1, 2, 3, 5}, {1, 2, 4, 5}, {1, 3, 4, 5}, {2, 3, 4, 5}}
>
> and I want a list pointing to all the sublists above that contain both
> a 2 and a 3. In this example I would get {1,2,5}. The best solution I
> have, with more generality, is:
>
> va = 2;
> vb = 3;
> za = Map[Cases[#1, va]&, ptX] /. {} -> {0}
> zb = Map[Cases[#1, vb]&, ptX] /. {} -> {0}
> Flatten[Position[za*zb, {va*vb}]]
>
> which gives
>
> {{2}, {2}, {2}, {0}, {2}}
> {{3}, {3}, {0}, {3}, {3}}
> {1, 2, 5}.
>
> (This doesn't work if va or vb is zero. That's ok.)
> There's probably a better way. Anyone? Thank you.
>
> Steve Gray
--
==========================================================
Curtis Osterhoudt
cfo at remove_this.lanl.and_this.gov
PGP Key ID: 0x4DCA2A10
Please avoid sending me Word or PowerPoint attachments
See http://www.gnu.org/philosophy/no-word-attachments.html
==========================================================
- References:
- Select from list
- From: Steve Gray <stevebg@roadrunner.com>
- Select from list