作业
任务一
请实现一个wordcount函数,统计英文字符串中每个单词出现的次数。返回一个字典,key为单词,value为对应单词出现的次数。
import re
from collections import defaultdict
def wordcount(text):
text = text.lower()
text = re.sub(r'[^\w\s]', '', text)
words = text.split()
word_count = defaultdict(int)
for word in words:
word_count[word] += 1
return dict(word_count)
text = """Hello world!
This is an example.
Word count is fun.
Is it fun to count words?
Yes, it is fun!"""
print(f"wordcount = {wordcount(text)}")
任务2
请使用本地vscode连接远程开发机,将上面你写的wordcount函数在开发机上进行debug,体验debug的全流程,并完成一份debug笔记(需要截图)。
笔记:
启动调试:
运行脚本,会在设置的断点处暂停。
检查变量:
在每个断点处检查变量的值,确认每一步的输出是否正确。
有问题的部分修改代码重新执行。
launch中配置:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
“version”: “0.2.0”,
“configurations”: [
{
"name": "Python 调试程序",
"type": "debugpy",
"request": "launch",
"python": "${command:python.interpreterPath}",
"console": "integratedTerminal"
}
]
}