def most_common(seq):
d = {}
for i in seq:
d[i] = d.get(i, 0) + 1
ret = []
for j in sorted(d.items(), reverse=True, key=lambda x:x[1]):
if len(ret) == 0:
ret.append(j[0])
n = j[1]
else:
if j[1] == n:
ret.append(j[0])
else:
break
return ret
print '频次最高', most_common([1,2,3,4,5,1,1,2,2])
最后
以上就是腼腆画板最近收集整理的关于python笔试题之找出一个列表里出现频次最高的元素(most common elements in a list)的全部内容,更多相关python笔试题之找出一个列表里出现频次最高的元素(most内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复