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

#以下是网上的一个牛人给你的解决方法

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



原文地址:https://www.cnblogs.com/think1988/p/4628254.html