[SciPy-user] finding values in a list?

Skipper Seabold jsseabold@gmail....
Sat Jul 11 16:29:29 CDT 2009


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
as in your example.

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


More information about the SciPy-user mailing list