基本上我只需要弄清楚如何從Python的列表中產生模式(最常出現的數字),無論該列表是否具有多種模式?如何在Python中生成多種模式?
事情是這樣的:
def print_mode (thelist):
counts = {}
for item in thelist:
counts [item] = counts.get (item, 0) + 1
maxcount = 0
maxitem = None
for k, v in counts.items():
if v > maxcount:
maxitem = k
maxcount = v
if maxcount == 1:
print "All values only appear once"
if counts.values().count (maxcount) > 1:
print "List has multiple modes"
else:
print "Mode of list:", maxitem
但不是在返回字符串「所有值只出現一次,」或「列表中有多種模式,」我希望它返回它的引用實際整數?
你想入手這款:http://stackoverflow.com/a/5829377/16363 – Mark 2013-02-10 01:09:24
啊,不,我已經看到了。我需要一些能夠產生模式和只有模式的東西,而不是其他的整數和它們出現的頻率? – hayleyelisa 2013-02-10 01:11:21
所以你只是想返回列表中的元素的頻率 – Harpal 2013-02-10 01:13:08