基本上我只需要弄清楚如何从 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
但不是在“所有值只出现一次”或“列表有多种模式”中返回字符串,而是希望它返回它引用的实际整数?