Numpy equivalent of list.index

前端 未结 6 2249
粉色の甜心
粉色の甜心 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:02

    The closest thing I could find to what you're asking for is nonzero. It may sound odd, but the documentation makes it look like it might have the desired result.

    http://www.scipy.org/Numpy_Example_List_With_Doc#nonzero

    Specifically this part:

    a.nonzero()

    Return the indices of the elements that are non-zero.

    Refer to numpy.nonzero for full documentation.

    See Also

    numpy.nonzero : equivalent function

    >>> from numpy import *
    >>> y = array([1,3,5,7])
    >>> indices = (y >= 5).nonzero()
    >>> y[indices]
    array([5, 7])
    >>> nonzero(y)                                # function also exists
    (array([0, 1, 2, 3]),)
    

    Where (http://www.scipy.org/Numpy_Example_List_With_Doc#where) may also be of interest to you.

提交回复
热议问题