中秋卷王:蒲语书生InterLM大模型实战闯关-入门岛-Python前置基础
一、Python实现wordcount
任务介绍
请实现一个wordcount函数,统计英文字符串中每个单词出现的次数。返回一个字典,key为单词,value为对应单词出现的次数。
Input:
"""Hello world!
This is an example.
Word count is fun.
Is it fun to count words?
Yes, it is fun!"""
Output:
{'hello': 1, 'world': 1, 'this': 1, 'is': 4, 'an': 1, 'example': 1, 'word': 1, 'count': 2,
'fun': 3, 'it': 2, 'to': 1, 'words': 1, 'yes': 1}
1、登录开发机
2、选择notebook python3环境
3、 输入文本模块
4、输入函数模块
5、输入执行模块
6、运行程序
二、Vscode链接InterStudio debug笔记
任务介绍
请使用本地vscode连接远程开发机,将上面你写的wordcount函数在开发机上进行debug,体验debug的全流程,并完成一份debug笔记(需要截图)。
1、本地vscode连接远程开发机
vscode安装SSH扩展
选择远程资源管理器,单机箭头选项
打开文件夹
选择/root目录
/root目录文件结构
2、使用本地vscode在远程开发机上创建py_debug_demo.py
新建文件夹,创建py文件
py_debug_demo文件代码
def wordcount(text):
# 将字符串转换为小写
text_lower = text.lower()
# 将字符串分割成单词(如果有标点则连在一起)
words = text_lower.split()
# 打印小写字符串
print(text_lower)
# 打印单词
print(words)
# 空字典存储每个单词的出现次数
count = {}
# 遍历单词
for word in words:
# 去除标点符号
word = word.strip('.,!?;"\'()')
# 若word不为空在统计单词
if word:
# 如果word之前没被记录则get返回0,如果之前被记录则get返回count相应的值
count[word] = count.get(word, 0) + 1
return count
# 测试代码
test_text = """Hello world!
This is an example.
Word count is fun.
Is it fun to count words?
Yes, it is fun!"""
print(wordcount(test_text))
3、运行结果
4、设置断点
5、运行调试
以hello为例分析
更新text输入文本
将text文本中的大写全部转变成小写
将小写文本进行单词之间的划分
count统计数组
记录单词hello
对hello进行去标点符号,发现没有,于是hello不变
count数组更新,将hello记录
以world!为例分析
记录单词world!
对world!进行去标点符号,发现有符号,于是world!变为world
count数组更新,将world记录
debug结束
debug最后一步,变量的全部情况