Python task
任务一
请实现一个wordcount函数,统计英文字符串中每个单词出现的次数。返回一个字典,key为单词,value为对应单词出现的次数。
代码如下
def wordcount(text):
# 使用正则表达式去除可能存在的标点符号并分割文本为单词列表
import re
words = re.findall(r'\b\w+\b', text.lower()) # 转换为小写以忽略大小写差异
# 初始化一个空字典来存储单词计数
word_count = {}
# 遍历单词列表,统计每个单词出现的次数
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
return word_count
# 示例
text = """Hello world!
This is an example.
Word count is fun.
Is it fun to count words?
Yes, it is fun!"""
print(wordcount(text))
任务二
请使用本地vscode连接远程开发机,将上面你写的wordcount函数在开发机上进行debug,体验debug的全流程,并完成一份debug笔记(需要截图)。
-
SSH远程连接到服务器(参考linux task)
-
安装python扩展,用于远程调试,下图已安装
-
打开编写好的
word_count.py
,打上断点 -
点击debug运行
-
程序在断点处停止,上方有单步调试和回退等按钮。