3 回答

TA贡献1856条经验 获得超11个赞
您可以使用max功能和键。看看使用'key'和lambda表达式的python max函数。
max(set(list), key=list.count)

TA贡献1786条经验 获得超11个赞
您可以使用具有-esque函数Counter的collections软件包中提供的mode
from collections import Counter
data = Counter(your_list_in_here)
data.most_common() # Returns all unique items and their counts
data.most_common(1) # Returns the highest occurring item
注意:Counter在python 2.7中是新的,在早期版本中不可用。

TA贡献1784条经验 获得超9个赞
从一些统计软件(即SciPy和MATLAB)中获取一个叶子,它们只会返回最小的最常用值,因此,如果两个值相等地频繁出现,则将返回其中最小的一个。希望有一个例子可以帮助:
>>> from scipy.stats import mode
>>> mode([1, 2, 3, 4, 5])
(array([ 1.]), array([ 1.]))
>>> mode([1, 2, 2, 3, 3, 4, 5])
(array([ 2.]), array([ 2.]))
>>> mode([1, 2, 2, -3, -3, 4, 5])
(array([-3.]), array([ 2.]))
有什么原因导致您无法遵守该约定?
添加回答
举报