闯关任务 Python实现wordcount 15mins
`import re
from collections import Counter
def wordcount(text):
# 使用正则表达式将文本转换为小写并分割成单词列表
words = re.findall(r'\b\w+\b', text.lower())
# 使用Counter来计算每个单词的出现次数
word_counts = Counter(words)
return dict(word_counts)
# 测试数据
text = """Hello world!
This is an example.
Word count is fun.
Is it fun to count words?
Yes, it is fun!"""
# 调用函数并打印结果
result = wordcount(text)
print(result)`