- 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', ...]
第四个断点开始对每个单词进行计数