书生大模型实战营——入门岛第2关

python实现单词统计

实现一个wordcount函数,统计英文字符串中每个单词出现的次数。返回一个字典,key为单词,value为对应单词出现的次数。

可以使用python中的正则表达式实现,每个单词由字母的引号组成:

import re


text = """
Got this panda plush toy for my daughter's birthday,
who loves it and takes it everywhere. It's soft and
super cute, and its face has a friendly look. It's
a bit small for what I paid though. I think there
might be other options that are bigger for the
same price. It arrived a day earlier than expected,
so I got to play with it myself before I gave it
to her.
"""


def wordcount(text):
    text = text.lower()
    words = re.findall(r"[a-z']+", text)
  
    # 使用字典存储
    word_dict = {}
    for word in words:
        if word in word_dict:
            word_dict[word] += 1
        else:
            word_dict[word] = 1
    return word_dict


print(wordcount(text))

Vscode连接InternStudio debug

配置Vscode环境

下面,使用vscode连接开发机,并进行debug。上一节中我们已经在Vscode中安装好了Romote-SSH插件,这里可以直接连接开发机。

开始调试代码前,在扩展中安装python插件:

在资源管理器中创建一个py文件,把上面的python代码拷贝进去:

执行前,选择开发机中的python解释器,直接使用conda中默认的base环境:

启动debug

在代码中添加断点后,选择左侧的运行的调试,点击Python Debugger

选择调试当前正在运行的Python文件

启动debug后的界面,左上角可以看见当前的变量的值:

点击继续或按F5可以跳到下一个断点,其他按钮还可以执行单步调试等等功能。

最终执行完后的结果:

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值