闯关任务一 Python实现wordcount
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=re.sub(r'[^\w\s]',' ',text.lower()).strip()
words=text.split()
word_count={}
for word in words:
if word in word_count:
word_count[word]+=1
else:
word_count[word]=1
return word_count
{'got': 2, 'this': 1, 'panda': 1, 'plush': 1, 'toy': 1, 'for': 3, 'my': 1, 'daughter': 1, 's': 3, 'birthday': 1, 'who': 1, 'loves': 1, 'it': 7, 'and': 3, 'takes': 1, 'everywhere': 1, 'soft': 1, 'super': 1, 'cute': 1, 'its': 1, 'face': 1, 'has': 1, 'a': 3, 'friendly': 1, 'look': 1, 'bit': 1, 'small': 1, 'what': 1, 'i': 4, 'paid': 1, 'though': 1, 'think': 1, 'there': 1, 'might': 1, 'be': 1, 'other': 1, 'options': 1, 'that': 1, 'are': 1, 'bigger': 1, 'the': 1, 'same': 1, 'price': 1, 'arrived': 1, 'day': 1, 'earlier': 1, 'than': 1, 'expected': 1, 'so': 1, 'to': 2, 'play': 1, 'with': 1, 'myself': 1, 'before': 1, 'gave': 1, 'her': 1}
闯关任务二 Vscode连接InternStudio debug笔记
重复上个关卡的操作,由于上次连接过,这次在Remote-SSH中可以连接。
但开发机中并没有Debug所用的扩展程序要先安装。
安装好之后,开始一行一行debug。重点关注旁边的locals。
首先,14行设置断点text输入成功。其实,这里注意到换行已经替换为“/n”,后面运行代码时并不影响,所以没有特意去处理。
"\nGot this panda plush toy for my daughter's birthday,\nwho loves it and takes it everywhere. It's soft and\nsuper cute, and its face has a friendly look. It's\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"
在15行设置断点,可以看到text正常将标点符号和空格替换为空格,字母也都变成小写。
'got this panda plush toy for my daughter s birthday \nwho loves it and takes it everywhere it s soft and\nsuper cute and its face has a friendly look it s\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'
继续在16行设置断点,成功将一段话变为单词列表。
['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', ...]
在for循环内不断重复if ,else,第一个got存入,且赋值为1,第二个this存入,且赋值为1,以此类推,重复值加一,没有的加进去。
-
1
: continue: 继续运行到下一个断点 -
2
: step over:跳过,可以理解为运行当前行代码,不进入具体的函数或者方法。 -
3
: step into: 进入函数或者方法。如果当行代码存在函数或者方法时,进入代码该函数或者方法。如果当行代码没有函数或者方法,则等价于step over。 -
4
: step out:退出函数或者方法, 返回上一层。 -
5
: restart:重新启动debug -
6
: stop:终止debug