[Numpy-discussion] Find insertion point

Nikolaus Rath Nikolaus@rath....
Tue Aug 17 13:21:16 CDT 2010

Warren Weckesser <warren.weckesser@enthought.com> writes:
> Nikolaus Rath wrote:
>> Hello,
>> I want to find the first i such that x[i] < y and x[i+1] >= y. Is there
>> a way to do this without using a Python loop?
>> I can't use np.searchsorted(), because my x array crosses y several
>> times.
> In [34]: np.where((x[:-1] < y) & (x[1:] > y))[0][0]
> Out[34]: 2

Neat, thanks!


 »Time flies like an arrow, fruit flies like a Banana.«

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C

More information about the NumPy-Discussion mailing list