""""
这个程序可以读取一个文本文件,统计每个单词出现的次数,并按照出现次数从高到低排序。程序可以用字典来记录每个单词出现的次数。
"""
with open("text.txt", "r") as f:
text = f.read()
# 将文本分割成单词
words = text.split()
# 初始化单词计数器
counts = {}
# 统计每个单词出现的次数
for word in words:
if word in counts:
counts[word] += 1
else:
counts[word] = 1
# 按照出现次数从高到低排序
sorted_counts = sorted(counts.items(), key=lambda x: x[1], reverse=True)
# 输出结果
for word, count in sorted_counts:
print(word, count)