4 回答
TA贡献1865条经验 获得超7个赞
collections.Counter
从标准库中使用
from collections import Counter
list_of_words = ['hello', 'hello', 'world']
lowercase_words = [w.lower() for w in list_of_words]
Counter(lowercase_words).most_common(1)
退货:
[('hello', 2)]
TA贡献1827条经验 获得超8个赞
首先规范化数据,使用 lower 或 upper。
li = ['COVID19', 'Covid19', 'covid19', 'coronavirus', 'Coronavirus']
li = [x.upper() for x in li] # OR, li = [x.lower() for x in li]
tag_counter = dict()
for tag in li:
if tag in tag_counter:
tag_counter[tag] += 1
else:
tag_counter[tag] = 1
popular_tags = sorted(tag_counter, key = tag_counter.get, reverse = True)
top_10 = popular_tags[:10]
print('\nList of the top 10 popular hashtags are :\n',top_10)
TA贡献1776条经验 获得超12个赞
见下文
from collections import Counter
lst = ['Ab','aa','ab','Aa','Cct','aA']
lower_lst = [x.lower() for x in lst ]
counter = Counter(lower_lst)
print(counter.most_common(1))
TA贡献1797条经验 获得超6个赞
您可以使用Counter收藏库
from collections import Counter
li = ['COVID19', 'Covid19', 'covid19', 'coronavirus', 'Coronavirus']
print(Counter([i.lower() for i in li]).most_common(10))
输出:
[('covid19', 3), ('coronavirus', 2)]
添加回答
举报