按出现次数从多到少排序

"""
   按出现次数从多到少排序
"""
filepath = 'd:/Documents/抖音昵称列表.txt'

names = []
f = open(filepath)
for line in f:
    names.append(line.strip())
f.close()

set01 = set(names)         # 转换成集合
# 每个元素都数一下有多少个
dict01 = {item:names.count(item) for item in set01}

sorted_x = sorted(dict01.items(), key=lambda x: x[1], reverse=True)

for item in  sorted_x:
    print(item)

关于李兴球

李兴球的博客是Python创意编程原创博客
此条目发表在python分类目录,贴了标签。将固定链接加入收藏夹。

发表回复