From: Peter Breitfeld on
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 &]
>
> 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.
>

You may use (list is your List)

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

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

--
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de

From: Scot T. Martin on
I think you're after the function SelectBy instead of Select and then
you're after the use of #[[3]] in place of #.

On Thu, 15 Jul 2010, 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 &]
>
> 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: Chris Poole on
Thanks. I actually solved the problem like so:

DeleteCases[Table[
If[Abs[0.1 - oldlist[[i, 3]]] <
0.05, {oldlist[[i, 1]], oldlist[[i, 2]],oldlist[[i, 3]]}]
, {i, 1, Length[oldlist]}], Null];

Seems very much like a hack though.

I don't appear to have a SelectBy function (I'm using mathematica
7...), but using Select on its own, with #[[3]] worked a charm. I
didn't realise I could still use the [[3]] notation here.

Many thanks.

On Thu, Jul 15, 2010 at 11:07 AM, Scot T. Martin
<smartin(a)seas.harvard.edu> wrote:
> I think you're after the function SelectBy instead of Select and then you=
're
> after the use of #[[3]] in place of #.
>
> On Thu, 15 Jul 2010, 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 &]
>>
>> 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: Patrick Scheibe on
Hi,

yes you can use Select. But with pattern matching and Cases it is
readable too:

l = {{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}};
Cases[l, {_, _, _?(Abs[# - 2.25] < 0.01 &)}]
Select[l, Abs[#[[3]] - 2.25] < 0.01 &]

Cheers
Patrick

On Thu, 2010-07-15 at 03:09 -0400, 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 &]
>
> 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: Bob Hanlon on

data = {{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[data, Abs[2.25 - #[[3]]] <= 0.02 &]

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

Cases[data, _?(Abs[2.25 - #[[3]]] <= 0.02 &)]

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

DeleteCases[data, _?(Abs[2.25 - #[[3]]] > 0.02 &)]

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


Bob Hanlon

---- Chris Poole <chris(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.