使用Python语言编写一个程序:
from collections import Counter
# 读取单词列表
words = []
while True:
try:
word = input()
words.append(word)
except:
break
# 计算单词频率
word_counts = Counter(words)
# 输出单词及其频率
for word, count in word_counts.items():
print(word, count)
该程序首先导入 collections
模块中的 Counter
函数,然后通过循环读取输入的单词列表,直到用户输入结束。接着,利用 Counter
函数计算单词的出现频率,并使用 items
方法按照字典序列返回单词及其出现次数。最后,循环遍历输出所有单词及其频率。由于要求不区分大小写,因此需要将所有单词转化为小写字母。