统计(数据处理)

1.获取数组中最多的元素(用max函数)

>>> a = [1,2,2,4,3,6,3,9]
>>> max(set(a),key = a.count)
2

很明显上面是漏掉了3,我觉得做数据还是要严谨的,2和3同样满足要求而丢弃3是非常愚蠢的做法。

2.使用函数Counter,可以迅速获取list中每个元素出现的次数

>>> from collections import Counter
>>> a
[1, 2, 2, 4, 3, 6, 3, 9]
>>> Counter(a)
Counter({2: 2, 3: 2, 1: 1, 4: 1, 6: 1, 9: 1})

计算列表中出现最多字符的方法地址https://www.cnblogs.com/Lands-ljk/p/5764003.html

原文地址:https://www.cnblogs.com/iBoundary/p/11407298.html