在NumPy数组中查找等于零的元素的索引 [英] Find indices of elements equal to zero in a NumPy array

查看:2716
本文介绍了在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])

这篇关于在NumPy数组中查找等于零的元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆