Numpy equivalent of list.index

前端 未结 6 2256
粉色の甜心
粉色の甜心 2021-02-12 11:48

In a low-level function that is called many times, I need to do the equivalent of python\'s list.index, but with a numpy array. The function needs to return when it finds the f

6条回答
  •  时光说笑
    2021-02-12 12:26

    If your numpy array is 1d array, maybe try like this:

    a = np.array([1, 2, 3])
    print a.tolist().index(2)
    >>> 1
    

    If is not 1d, you could search trough array like:

    a = np.array([[1, 2, 3],[2,5,6],[0,0,2]])
    print a[0,:].tolist().index(2)
    >>> 1
    
    print a[1,:].tolist().index(2)
    >>> 0
    
    print a[2,:].tolist().index(2)
    >>> 2
    

提交回复
热议问题