1.
请实现一个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}
原码:
import re
d = {}
text = str(input())
def Word_count(text):
words = re.findall("[a-zA-Z]+", text.lower())
for key in words:
if d.get(key)!=None:
d[key]+=1
else:
d[key] = 1
print(d)
Word_count(text)
2.dubug笔记:
使用vscode远程连接开发机对root文件夹里的py文件进行debug,通过断点,逐过程运行之后完成了word_count程序