L1-Python 任务记录
wordcount 任务
完整代码
def wordcount(input_str):
#定义字典
word_dict = {}
#实现全体字母小写
input_str = input_str.lower()
#去除标点符号
punction = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
for i in punction:
input_str = input_str.replace(i,' ')
# 将字母拆解成word list
word_list = input_str.split()
print(word_list)
#统计单词
for word in word_list:
if word not in word_dict:
word_dict[word] = 0
else:
word_dict[word] +=1
return word_dict
input = """Hello world!
This is an example.
Word count is fun.
Is it fun to count words? Yes, it is fun!"""
output = wordcount(input)
print(output)
输出结果
(base) root@intern-studio-50015995:~# python debug.py
['hello', 'world', 'this', 'is', 'an', 'example', 'word', 'count', 'is', 'fun', 'is', 'it', 'fun', 'to', 'count', 'words', 'yes', 'it', 'is', 'fun']
{'hello': 0, 'world': 0, 'this': 0, 'is': 3, 'an': 0, 'example': 0, 'word': 0, 'count': 1, 'fun': 2, 'it': 1, 'to': 0, 'words': 0, 'yes': 0}
调试记录:
- 断点 设置的3个点用于监控字符串处理中的变换情况
单步调试发现大写字符全变成小写
标点逐个去除
字符串拆解成word list,方便统计
调试技术部分观察各个key的变换情况