Searching Using np.where( ) :
The where( ) function returns the indices of elements that satisfy a condition .
Syntax :
Python
np.where(condition) Example :
Python
import numpy as np arr = np.array([10, 20, 30, 20, 40]) result = np.where(arr == 20) print(result) #output (array([1, 3]),) Searching with Conditions :
Python
import numpy as np arr = np.array([10, 20, 30, 20, 40]) result = np.where(arr > 25) print(result) #output (array([2, 4]),) Searching in 2D Arrays :
Python
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) result = np.where(arr == 5) print(result) #output (array([1]), array([1])) Searching Using np.searchsorted( ) :
searchsorted( ) is used to find the index where a value should be inserted in a sorted array .
Syntax :
Python
np.searchsorted(array, value) Example :
Python
import numpy as np arr = np.array([10, 20, 30, 40, 50]) index = np.searchsorted(arr, 35) print(index) #output 3