书生大模型训练营-L0关卡-Python

  • Python实现wordcount
  • Vscode连接InternStudio debug笔记

Python实现wordcount

题目要求:

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

Eg:

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}

TIPS:记得先去掉标点符号,然后把每个单词转换成小写。不需要考虑特别多的标点符号,只需要考虑实例输入中存在的就可以。

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):
    pass

 代码实现:

import re
from collections import defaultdict

def wordcount(text):
    # 去除标点符号
    text = re.sub(r'[^\w\s]', '', text)
    # 转换为小写
    text = text.lower()
    # 拆分为单词
    words = text.split()
    # 统计单词频率
    word_count = defaultdict(int)
    for word in words:
        word_count[word] += 1
    return dict(word_count)

# text = """
# Hello world!
# This is an example.
# Word count is fun.
# Is it fun to count words?
# Yes, it is fun!
# """
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.
"""

result = wordcount(text)
print(result)

Vscode连接InternStudio debug笔记

远程连接开发机,并使用命令行的方式进行debug。首先点击左侧边栏的debug图标,选择“创建launch.json文件”

在上方弹出的窗口中,选择debugger时选择python debuger,选择debug config时选择remote attach。随后会让我们选择debug server的地址,因为我们是在本地debug,所以全都保持默认直接回车就可以了。

配置完以后会打开配置的json文件,可以关掉。这时可以看到run and debug界面有变化,左上角出现了绿色的debug选项。

然后在命令行中输入以下命令:

python -m debugpy --listen 5678 --wait-for-client ./count.py

如果报错 no module named debugpy

说明没有安装对应的依赖包,使用命令:

pip install debugpy

成功后,添加断点,点击左上角的绿色箭头开启debug

可以看到,第一个断点停在了去除标点符号后的text

'\nGot this panda plush toy for my daughters birthday\nwho loves it and takes it everywhere Its soft and\nsuper cute and its face has a friendly look Its\na bit small for what I paid though I think there\nmight be other options that are bigger for the\nsame price It arrived a day earlier than expected\nso I got to play with it myself before I gave it\nto her\n'

第二个断点停在转换为小写后的text

 '\ngot this panda plush toy for my daughters birthday\nwho loves it and takes it everywhere its soft and\nsuper cute and its face has a friendly look its\na bit small for what i paid though i think there\nmight be other options that are bigger for the\nsame price it arrived a day earlier than expected\nso i got to play with it myself before i gave it\nto her\n'

 第三个断点停在拆分后的word数组:

['got', 'this', 'panda', 'plush', 'toy', 'for', 'my', 'daughters', 'birthday', 'who', 'loves', 'it', 'and', 'takes', 'it', 'everywhere', 'its', 'soft', 'and', 'super', 'cute', 'and', 'its', 'face', 'has', 'a', 'friendly', 'look', 'its', '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', ...]

第四个断点开始对每个单词进行计数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值