You can search an array for a specific value and obtain the indices of matches using the where() method.
Locate the indices where the value is 4:
import numpy as np arr = np.array([1, 2, 3, 4, 5, 4, 4]) x = np.where(arr == 4) print(x) |
The previous example will return a tuple: (array([3, 5, 6],), indicating that the value 4 is located at indices 3, 5, and 6.
Locate the indices where the values are even:
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) x = np.where(arr%2 == 0) print(x) |
Locate the indices where the values are odd:
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) x = np.where(arr%2 == 1) print(x) |