Re: Select from list
- To: mathgroup at smc.vnet.net
- Subject: [mg87749] Re: [mg87729] Select from list
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Wed, 16 Apr 2008 06:50:45 -0400 (EDT)
- References: <200804160904.FAA23961@smc.vnet.net>
On 16 Apr 2008, at 18:04, 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 > Flatten[Position[ptX, {___, 2, ___, 3, ___}]] {1, 2, 5} That's assuming that 3 comes always after 2 in your list. If not: Flatten[Position[ptX, {___, 2, ___, 3, ___} | {___, 3, ___, 2, ___}]] Andrzej Kozlowski
- References:
- Select from list
- From: Steve Gray <stevebg@roadrunner.com>
- Select from list