The searchsorted() method performs a binary search on the array and returns the index where the specified value should be inserted to preserve the order.
The searchsorted() method is intended for use with sorted arrays. |
Determine the indices where the value 7 should be inserted:
import numpy as np arr = np.array([6, 7, 8, 9]) x = np.searchsorted(arr, 7) print(x) |
Example explained: The number 7 should be inserted at index 1 to maintain the sort order.
The method searches from the left and returns the first index where 7 is not larger than the next value.