- Array can be searched to look for a certain value.
- Searching any array in NumPy returns the indexes of the place where the given value is present.
- where()
- searchsorted() is another NumPy array function that performs the binary search in an array.
- returns index where the specified value would be inserted to maintain the search order
- assumed to be used on sorted arrays
- By default, it returns the left-most index
- To return right-most index,
np.searchsorted(array, No. to search, side= 'right')
- To search for multiple values in an array, an array with specified values is provided.
arr = np.array([1,4,6,8])
np.searchsorted(arr, [1, 2, 3, 4, 6])
Since, 2 & 3 both values are missing from the array hence, both will be inserted between 1 & 4, and the output will be;
Output:
[1 1 2 3 4]