I have a NumPy array, and I want to find the index of the first occurrence of a specific element in it. Is there a built-in NumPy function to do this, or do I need to write my own function?
Here is an example of my array:
import numpy as np
arr = np.array([4, 2, 3, 1, 4])
I want to find the index of the first occurrence of the value 4, which should be 0.
I tried using the numpy.where()
function, but it returns a tuple of arrays and I’m not sure how to extract the first element:
>>> np.where(arr == 4)
(array([0, 4]),)