I know there is a method for a Python list to return the first index of something:
>>> l = [1, 2, 3] >>> l.index(2) 1
Is there something like that for NumPy arrays?
You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
The index() method returns the index of the given element in the list. If the element is not found, a ValueError exception is raised.
Yes, given an array, array
, and a value, item
to search for, you can use np.where
as:
itemindex = numpy.where(array==item)
The result is a tuple with first all the row indices, then all the column indices.
For example, if an array is two dimensions and it contained your item at two locations then
array[itemindex[0][0]][itemindex[1][0]]
would be equal to your item and so would be:
array[itemindex[0][1]][itemindex[1][1]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With