您的位置:首页 > 编程语言 > Python开发

输出列表中出现次数最多的元素 分类: python 2013-01-15 15:25 990人阅读 评论(0) 收藏

2013-01-15 15:25 746 查看
#以下是网上的一个牛人给你的解决方法

from collections import Counter

a=['bj', 'bj', 'bj', 'gz', 'shh', 'shh']

d=Counter(a)

print d

print d.most_common()

print d.most_common()[0]

print d.most_common()[0][0]

方法二:

一个网友的写法:

def sortbylistcount(self,list):

cnt=0

city=""

for x in list:

if list.count(x)>cnt:

city=x

cnt=list.count(x)

return city

方法三:

for n in a:

old=a.count(n)

num=max(a.count(m) for m in a)

if num==old:

print n

break
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐