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

前端 未结 13 1866
萌比男神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条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 06:31

    You can also convert a NumPy array to list in the air and get its index. For example,

    l = [1,2,3,4,5] # Python list
    a = numpy.array(l) # NumPy array
    i = a.tolist().index(2) # i will return index of 2
    print i
    

    It will print 1.

提交回复
热议问题