# [SciPy-user] finding values in a list?

Carlos "Guâno" Grohmann carlos.grohmann@gmail....
Mon Jul 13 09:04:45 CDT 2009

```Thanks all for the help

Carlos

On Sat, Jul 11, 2009 at 18:32, <josef.pktd@gmail.com> wrote:
> On Sat, Jul 11, 2009 at 5:29 PM, Skipper Seabold<jsseabold@gmail.com> wrote:
>> On Sat, Jul 11, 2009 at 5:22 PM, <josef.pktd@gmail.com> wrote:
>>> 2009/7/11 Carlos "Guâno" Grohmann <carlos.grohmann@gmail.com>:
>>>> Sorry if this is too simple or even OT, but say I have a list like
>>>>
>>>> [1,2,3,4,5,6,7,8,9]
>>>>
>>>> and I want to get the values just below and above a given value.
>>>>
>>>> So, if I enter 6.2, I get 6 and 7.
>>>>
>>>> but how?
>>>
>>> maybe like this
>>>>>> a = (np.array([1,2,3,4,5,6,7,8,9])-6.1)
>>>>>> a[a>0].min() + 6.2
>>> 7.0
>>>>>> a[a<0].max() + 6.2
>>> 6.0
>>>
>>> Josef
>>>
>>
>> Yet another solution (though I too don't know if it's optimal).  This
>> will work if you know that your values are always going to be within 1
>>
>>>>> import numpy as np
>>>>> L = [1,2,3,4,5,6,7,8]
>>>>> A = np.array(L)
>>>>> index = np.where(np.abs((A-6.2))<1)
>>>>> A[index]
>> array([6, 7])
>>
>> Skipper
>> _______________________________________________
>> SciPy-user mailing list
>> SciPy-user@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>
> for sorted lists, this might be fastest
>
>>>> ind = np.searchsorted([1,2,3,4,5,6,7,8,9], 6.2)
>>>> ind
> 6
>>>> [1,2,3,4,5,6,7,8,9][ind-1]
> 6
>>>> [1,2,3,4,5,6,7,8,9][ind]
>
> It is unclear what should happen if the values is an element of the list.
>
> Josef
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>

--
Carlos Henrique Grohmann - Geologist D.Sc.
a.k.a. Guano - Linux User #89721
ResearcherID: A-9030-2008

http://digitalelevation.blogspot.com

http://www.igc.usp.br/pessoais/guano
_________________
Can’t stop the signal.
```