def wordcount(text):
# 将文本分割成单词列表,并转换为小写
words = text.lower().split()
# 初始化一个空字典用于存储单词计数
word_counts = {}
# 遍历单词列表中的每个单词
for word in words:
# 如果单词在字典中,则计数加1,否则将单词加入字典并设为1
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
return word_counts
# 测试函数
text = """Hello world!
This is an example.
Word count is fun.
Is it fun to count words?
Yes, it is fun!"""
word_counts = wordcount(text)
print(word_counts)
ssh链接
安装插件
运行
程序运行结果