|
[Date Index]
[Thread Index]
[Author Index]
Re: Select only certain sublists matching criteria
- To: mathgroup at smc.vnet.net
- Subject: [mg110992] Re: Select only certain sublists matching criteria
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Fri, 16 Jul 2010 05:16:52 -0400 (EDT)
On 7/15/10 at 3:09 AM, chris at 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.
Prev by Date:
Changing the default width of Exported cells
Next by Date:
Re: Select only certain sublists matching criteria
Previous by thread:
Re: Select only certain sublists matching criteria
Next by thread:
Re: Select only certain sublists matching criteria
|