[SciPy-user] finding values in a list?
Sat Jul 11 16:32:10 CDT 2009
On Sat, Jul 11, 2009 at 5:29 PM, Skipper Seabold<email@example.com> wrote:
> On Sat, Jul 11, 2009 at 5:22 PM, <firstname.lastname@example.org> wrote:
>> 2009/7/11 Carlos "Guâno" Grohmann <email@example.com>:
>>> Sorry if this is too simple or even OT, but say I have a list like
>>> 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
>>>>> a[a<0].max() + 6.2
> 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)
> array([6, 7])
> SciPy-user mailing list
for sorted lists, this might be fastest
>>> ind = np.searchsorted([1,2,3,4,5,6,7,8,9], 6.2)
It is unclear what should happen if the values is an element of the list.
More information about the SciPy-user