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

前端 未结 13 1629
萌比男神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:33

    Note: this is for python 2.7 version

    You can use a lambda function to deal with the problem, and it works both on NumPy array and list.

    your_list = [11, 22, 23, 44, 55]
    result = filter(lambda x:your_list[x]>30, range(len(your_list)))
    #result: [3, 4]
    
    import numpy as np
    your_numpy_array = np.array([11, 22, 23, 44, 55])
    result = filter(lambda x:your_numpy_array [x]>30, range(len(your_list)))
    #result: [3, 4]
    

    And you can use

    result[0]
    

    to get the first index of the filtered elements.

    For python 3.6, use

    list(result)
    

    instead of

    result
    
    0 讨论(0)
提交回复
热议问题