3 回答

TA貢獻1856條經(jīng)驗 獲得超11個贊
您可以使用max功能和鍵??纯词褂?#39;key'和lambda表達式的python max函數(shù)。
max(set(list), key=list.count)

TA貢獻1786條經(jīng)驗 獲得超11個贊
您可以使用具有-esque函數(shù)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條經(jīng)驗 獲得超9個贊
從一些統(tǒng)計軟件(即SciPy和MATLAB)中獲取一個葉子,它們只會返回最小的最常用值,因此,如果兩個值相等地頻繁出現(xiàn),則將返回其中最小的一個。希望有一個例子可以幫助:
>>> 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.]))
有什么原因?qū)е履鸁o法遵守該約定?
添加回答
舉報