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

前端 未结 13 1883
萌比男神i
萌比男神i 2020-11-22 05:55

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

13条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 06:23

    There are lots of operations in NumPy that could perhaps be put together to accomplish this. This will return indices of elements equal to item:

    numpy.nonzero(array - item)
    

    You could then take the first elements of the lists to get a single element.

提交回复
热议问题