查找列表中出现次数最多的元素

点击查看代码
words = ['apple','pen','hi','banana','apple','hi','orange','apple','pen']
#导入Counter模块
from collections import Counter
count_words =Counter(words)

count_words.most_common()
#[('apple', 3), ('pen', 2), ('hi', 2), ('banana', 1), ('orange', 1)]
count_words.most_common(1) 
#[('apple', 3)]
posted @ 2022-03-23 16:18  xxchenwei  阅读(23)  评论(0)    收藏  举报