单词出现频率统计

所属作业: hw9

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
n = int(input())
word_dict = {}
# 统计单词出现频率
for _ in range(n):
    word = input()
    word_dict[word] = word_dict.get(word, 0) + 1
# 对单词频率进行排序
sorted_words = sorted(word_dict.items(), key=lambda x: (-x[1], x[0]))
for word, count in sorted_words:
    print(f"{count} {word}")

word_dict[’about’]=2

word_dict[’me’]=1

word_dict.items()

word_dict.items()→ [(’about’,2),(’me’,1)]

x → (’about’,2) → (-2,’about’)