中秋卷王:蒲语书生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最后一步,变量的全部情况
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值