[SciPy-user] finding values in a list?

Alan McIntyre alan.mcintyre@gmail....
Sat Jul 11 16:22:02 CDT 2009


On Sat, Jul 11, 2009 at 2:21 PM, Alan McIntyre<alan.mcintyre@gmail.com> wrote:
> There's probably more efficient ways to do it, but this works:
>
> def get_nearest(a, v):
>     '''Get the values in 'a' that bracket 'v'.  Assumes a is a list
> already sorted in ascending order.'''
>     A = np.array(a)
>     return A[A < v][-1], A[A > v][0]
>
> get_nearest([1,2,3,4,5,6,7,8,9], 6.2)
> [6, 7]
>

Sorry, I forgot to include an "import numpy as np" in there. :)


More information about the SciPy-user mailing list