任务一:实现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}
模板如下:
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
首先我们先去掉标点符号,然后把每个单词转换成小写。这里采用正则模块,也是我们在LLM中经常会用到的模块,当然这里也可以使用replace,但是replace方法每次只能对一个标点符号进行替换,不是很方便。
# 将字符串转为小写
text=text.lower()
#使用正则化模块替换所有不是字母数字和空白字符的string
import re
text = re.sub(r'[^\w\s]', '', text)
#使用split方法分割text
for n in text.split():
print(n)
--->>>
got
this
panda
plush
toy
...
gave
it
to
her
解题思路:
我们用用一个字典来存储每个单词,这里注意key为每个单词,value为每个单词出现的次数。
接下来我们开始写wordcount函数。可以看到很好的完成了我们的任务。
def wordcount(text):
wordict = {}
for word in text.split():
if word in wordict:
wordict[word] += 1
else:
wordict[word] = 1
return wordict
wordcount(text)
--->>>
{'got': 2,
'this': 1,
'panda': 1,
'plush': 1,
'toy': 1,
'for': 3,
'my': 1,
'daughters': 1,
'birthday': 1,
'who': 1,
'loves': 1,
'it': 5,
'and': 3,
'takes': 1,
'everywhere': 1,
'its': 3,
'soft': 1,
'super': 1,
'cute': 1,
'face': 1,
'has': 1,
'a': 3,
'friendly': 1,
'look': 1,
'bit': 1,
...
'with': 1,
'myself': 1,
'before': 1,
'gave': 1,
'her': 1}
可以看到很好的完成了我们的任务。
任务二:Debug的学习使用
我们可以在某个代码行打上断点,当代码运行到这里时,它会停下来,这样你就可以检查变量的值、执行步骤等。
之后我们使用VS Code里的调试来运行当前代码文件。
可以在左侧看到每次执行变量的值。在这里可以进行查看和更改,帮助你更好的完成你的代码