From: Chris Poole on
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.

From: Bill Rowe on
On 7/15/10 at 3:09 AM, chris(a)chrispoole.com (Chris Poole) 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 &]

Change # to #[[3]] and the selection criteria will be applied to
the 3rd element of each item in the list

>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.

Here are a few ways to do what you want for the data above after
setting the tolerance to 0.01. I changed the tolerance since
there is nothing within 0.001 of 2.25 in your data

In[3]:= Select[data, Abs[#[[3]] - 2.25] < .01 &]

Out[3]= {{3, -4, 2.255}, {3.5, -4, 2.242}}

In[4]:= Select[data, Abs[Last@# - 2.25] < .01 &]

Out[4]= {{3, -4, 2.255}, {3.5, -4, 2.242}}

In[5]:= Cases[data, {_, _, _?(Abs[# - 2.25] < .01 &)}]

Out[5]= {{3, -4, 2.255}, {3.5, -4, 2.242}}

In[7]:= Pick[data, data[[All, 3]], _?(Abs[# - 2.25] < .01 &)]

Out[7]= {{3, -4, 2.255}, {3.5, -4, 2.242}}

This last approach is most useful when you already have the
element of each sublist you want as your selector in a separate
list. If this is not the case, then the overhead required to
create the second argument to Pick will make this method slower
than the others for long lists.


From: Raffy on
On Jul 15, 12:09 am, Chris Poole <ch...(a)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]&]

From: David Park on
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}};

Select[list, (2.24 <= Part[#, 3] <= 2.26) &]

{{3, -4, 2.255}, {3.5, -4, 2.242}}


David Park
djmpark(a)comcast.net
http://home.comcast.net/~djmpark/


From: Chris Poole [mailto:chris(a)chrispoole.com]


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.



From: Albert Retey on
Am 15.07.2010 09:09, schrieb Chris Poole:
> 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.

I think this should work:

Select[lst, Abs[#[[3]] - 2.25] < 0.01 &]

hth,

albert