numpy.searchsorted(a, v, side='left', sorter=None)
[source]
Find indices where elements should be inserted to maintain order.
Find the indices into a sorted array a
such that, if the corresponding elements in v
were inserted before the indices, the order of a
would be preserved.
Assuming that a
is sorted:
side | returned index i satisfies |
---|---|
left | a[i-1] < v <= a[i] |
right | a[i-1] <= v < a[i] |
Parameters: |
|
---|---|
Returns: |
|
Binary search is used to find the required insertion points.
As of NumPy 1.4.0 searchsorted
works with real/complex arrays containing nan
values. The enhanced sort order is documented in sort
.
This function uses the same algorithm as the builtin python bisect.bisect_left
(side='left'
) and bisect.bisect_right
(side='right'
) functions, which is also vectorized in the v
argument.
>>> np.searchsorted([1,2,3,4,5], 3) 2 >>> np.searchsorted([1,2,3,4,5], 3, side='right') 3 >>> np.searchsorted([1,2,3,4,5], [-10, 10, 2, 3]) array([0, 5, 1, 2])
© 2005–2019 NumPy Developers
Licensed under the 3-clause BSD License.
https://docs.scipy.org/doc/numpy-1.17.0/reference/generated/numpy.searchsorted.html