Nope Nope - 1 month ago 10
Python Question

Is there a Numpy function to return the first index of something in an array?

I know there is a method for python list to return the first index of something

l = list(1,2,3)
l.index(2)
>>> 1


Is there something like that for numpy arrays?

Answer

Yes, here is the answer given a Numpy array, array, and a value, item, to search for.

itemindex = numpy.where(array==item)

The result is a tuple with first all the row indices, then all the column indices.

For example if 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

array[itemindex[0][1]][itemindex[1][1]]

numpy.where

Comments