MathGroup Archive 2010

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

Search the Archive

Re: Select only certain sublists matching criteria

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110995] Re: Select only certain sublists matching criteria
  • From: Raffy <adraffy at gmail.com>
  • Date: Fri, 16 Jul 2010 05:17:24 -0400 (EDT)
  • References: <i1mc7n$8u2$1@smc.vnet.net>

On Jul 15, 12:09 am, Chris Poole <ch... at chrispoole.com> wrote:
> I have a list like this:
> {{0, -4, 2.283}, {0.5, -4, 2.282}, {1, -4, 2.280}, {1.5, -4,
>   2.276}, {2, -4, 2.271}, {2.5, -4, 2.264}, {3, -4, 2.255}, {3.5, -4,
>   2.242}}
>
> I want to look at each list of 3 numbers, and keep only the lists where the third item in each list fits some criteria.
>
> I can do something like this:
> Select[{2, 15, 1, 16, 17}, Abs[3 - #] < 3 &]
>
> But it only works for flat lists.
>
> For example, I want only the lists where the third item is around 2.25 +- 0.001. Something like that.
>
> I can work out how to get Select to operate on the 3rd item of each sublist, but not how to then keep that entire list.
>
> If anyone has any ideas, they are much appreciated.

list = {{0, -4, 2.283}, {0.5, -4, 2.282}, {1, -4, 2.280}, {1.5, -4,
2.276}, {2, -4, 2.271}, {2.5, -4, 2.264}, {3, -4, 2.255}, {3.5, -4,
2.242}}
test = 3rd argument x when evaluated as f(x) returns true or false

There are lots of ways to do this:

Select[list, f[#[[3]]]&]

Pick[list, f/@list[[All,3]]]

Extract[list, Position[list[[All, 3]], x_/;f[x], {1}, Heads->False]]


You could generalize this to new select function that applies (instead
of maps) the test function onto each element.

ClearAll[mapSelect];
mapSelect[list_, crit_] := Pick[list, crit/@list];

mapSelect is effectively the same as Select.

ClearAll[applySelect];
applySelect[list_, crit_] := Pick[list, crit@@@list];

For your problem, you could then use:  applySelect[list, f[#3]&]


  • Prev by Date: Re: Select only certain sublists matching criteria
  • Next by Date: Re: Accessing static members with J/Link
  • Previous by thread: Re: Select only certain sublists matching criteria
  • Next by thread: Re: Select only certain sublists matching criteria