Skip to main content
 首页 » 编程设计

python之查找数据中零元素的数量

2025年05月04日9kenshinobiy

我有代码:

def find_zeros(data): 
    '''creates a list of the indexes of the zeros in the data''' 
    zeroidx=np.where(np.any(data==0, axis=1)) 
    print zeroidx 
    return len(zeroidx)  

但结果是:

(array([525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 
       538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 
       551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 
       564, 565], dtype=int64),) 

给出长度1。

如何找到数字数组的长度?

这会找到零点的位置。基本上我只想知道数据中有多少个零? 还有其他更好的方法吗?

请您参考如下方法:

len(data) - numpy.count_nonzero(data) 

有一个内置的。