- 任务一:
要求:请实现一个wordcount函数,统计英文字符串中每个单词出现的次数。返回一个字典,key为单词,value为对应单词出现的次数。
import re
from collections import Counter
def wordcount(text):
# 用正则表达式替换标点符号为空格
cleaned_text = re.sub(r'[^\w\s]', ' ', text)
# 将所有单词转换为小写
cleaned_text = cleaned_text.lower()
# 分割字符串为单词列表
words = cleaned_text.split()
# 使用Counter计算每个单词出现的次数
word_count = Counter(words)
return dict(word_count)
VScode运行结果:
- 任务二:
要求:Vscode连接InternStudio debug笔记
python -m pdb word_count.py
# (Pdb) 会自动停在第一行,等待调试
断点设置:(Pdb)12 #断点设置在word_count.py第12行
运行:(Pdb)c #跳到下个断点