numpy如何查找数组中个数最多的元素
2个回答
展开全部
import numpy as np
b=np.array([[0, 4, 4],[2, 0, 3],[1, 3, 4]])
print('b=')
print(b)
l=sorted([(np.sum(b==i),i) for i in set(b.flat)])
'''
np.sum(b==i) #统计b中等于i的元素个数
set(b.flat) #将b转为一维数组后,去除重复元素
sorted() #按元素个数从小到大排序
l[-1] #取出元素个数最多的元组对 (count,element)
'''
print('max times of element in b is {1} with {0} times'.format(*l[-1]))
[willie@localhost pys]$ python3 countnumpy.py
b=
[[0 4 4]
[2 0 3]
[1 3 4]]
max times of element in b is 4 with 3 times
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询