闯关任务
任务描述:Python task
任务文档:
完成结果
- Python实现wordcount
请实现一个wordcount函数,统计英文字符串中每个单词出现的次数。返回一个字典,key为单词,value为对应单词出现的次数。
函数:
import re
from collections import Counter
def wordcount(text):
# 使用正则表达式移除标点符号并分割单词
words = re.findall(r'\b\w+\b', text.lower())
# 计数每个单词出现的次数
word_counts = Counter(words)
return dict(word_counts)
- Vscode连接InternStudio debug笔记
采用debug测试,发现和其他实战营学员相同的问题,“'s”中的“s”会被单独计数:
通过修改代码并重新debug,发现该问题被解决: