# 中文
txt = input("请输入一段文本:")
d = {}
for i in txt:
d[i] = d.get(i, 0) + 1 # 字典中的值
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True) # 排序
for i in range(len(d)):
word, count = ls[i]
print("{:<10}{:<5}".format(word, count))

该博客展示了如何用Python处理用户输入的文本,统计每个字符出现的次数,并进行降序排序。通过字典存储每个字符的计数,然后使用列表排序功能展示结果。内容涉及Python基础和文本处理技巧。
# 中文
txt = input("请输入一段文本:")
d = {}
for i in txt:
d[i] = d.get(i, 0) + 1 # 字典中的值
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True) # 排序
for i in range(len(d)):
word, count = ls[i]
print("{:<10}{:<5}".format(word, count))

1229
1万+
1万+
2万+

被折叠的 条评论
为什么被折叠?