Find indices of elements equal to zero in a NumPy array

前端 未结 8 1315
孤城傲影
孤城傲影 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: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])
    

提交回复
热议问题