这是
defaultdict适用于Python 2.5及更高版本的解决方案:
from collections import defaultdictL = [1,2,45,55,5,4,4,4,4,4,4,5456,56,6,7,67]d = defaultdict(int)for i in L: d[i] += 1result = max(d.iteritems(), key=lambda x: x[1])print result# (4, 6)# The number 4 occurs 6 times
请注意,是否
L = [1, 2, 45, 55, 5, 4, 4, 4, 4, 4, 4, 5456, 7, 7, 7, 7, 7, 56, 6, 7,67]有六个4s和六个7s。但是,结果将是
(4, 6)六个4s。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)