python 统计数组中某个元素的个数

import numpy as np
a = np.random.randint(-5, 5, (1, 10))
c=np.sum(a>=1)                    #条件为大于等于1
print ("随机数组a  :  "+str(a)) #输出数组a
print ("大于等于1的个数:  "+str(c)) #输出满足条件的个数

例如

随机数组a  :  [[-1  2  4  1 -1 -5 -5  2  2  4]]
大于等于1的个数:  6

假如是统计某个值(比如1这个值)有多少个:

c=np.sum(a==1) 


原文地址:https://www.cnblogs.com/yibeimingyue/p/15440761.html