在NumPy数组中查找等于零的元素的索引
问题描述:
NumPy具有高效的功能/方法 nonzero()
标识ndarray
对象中非零元素的索引.获取要做的值为零的元素的索引的最有效方法是什么?
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 the indices of the elements that do have a value of zero?
答
numpy.where ()是我的最爱.
>>> x = numpy.array([1,0,2,0,3,0,4,5,6,7,8])
>>> numpy.where(x == 0)[0]
array([1, 3, 5])