Find indices of elements equal to zero in a NumPy array

前端 未结 8 1306
孤城傲影
孤城傲影 2020-11-29 17:28

NumPy has the efficient function/method nonzero() to identify the indices of non-zero elements in an ndarray object. What is the most efficient way to obtain th

相关标签:
8条回答
  • 2020-11-29 17:56

    numpy.where() is my favorite.

    >>> x = numpy.array([1,0,2,0,3,0,4,5,6,7,8])
    >>> numpy.where(x == 0)[0]
    array([1, 3, 5])
    
    0 讨论(0)
  • 2020-11-29 17:58

    If you are working with a one-dimensional array there is a syntactic sugar:

    >>> x = numpy.array([1,0,2,0,3,0,4,5,6,7,8])
    >>> numpy.flatnonzero(x == 0)
    array([1, 3, 5])
    
    0 讨论(0)
提交回复
热议问题