任务:
1. 实现一个wordcount函数,统计英文字符串中每个单词出现的次数。返回一个字典,key为单词,value为对应单词出现的次数。
TIPS:记得先去掉标点符号,然后把每个单词转换成小写。不需要考虑特别多的标点符号,只需要考虑实例输入中存在的就可以。
2. 使用本地vscode连接远程开发机,将上面你写的wordcount函数在开发机上进行debug,体验debug的全流程,并完成一份debug笔记(需要截图)。
任务一
python关键代码
1. 先去掉标点符号,只需要考虑实例输入中存在的
import string
trans = str.maketrans('', '', string.punctuation)
text=text.translate(trans)
利用 string 模块中的 punctuation 字符串,它包含了所有的标点符号,然后使用 str.translate() 方法来删除这些字符。
2. 输入的字段包含大小写,将所有字符转成小写
text = text.lower()
3. 字符串分割为列表
ws = text.split()
w_dict = {}
for word in ws:
if word in w_dict:
w_dict[word]+=1
else:
w_dict[word] = 1
以空格为分割符的函数,遍历列表,如果存在该字符串则count+1,否则创建并值为1。
具体实现如下:
import string ,re
text="""Hello world!
This is an example.
Word count is fun.
Is it fun to count words?
Yes, it is fun!"""
trans = str.maketrans('', '', string.punctuation)
text=text.translate(trans)
text = text.lower()
ws = text.split()
w_dict = {}
for word in ws:
if word in w_dict:
w_dict[word]+=1
else:
w_dict[word] = 1
print(w_dict)
输出:
任务二
1..连接服务器并打开调试
2. 点击左边的运行与调试(或右上角选择下图所示选项)
3. 左上角为运行到当前断点状态下的参数信息,包括local和global的参数,global参数
4. 同时可以右键进行针对监控,可以看到word的参数会随着单步调试而改变。