L1-Python 任务记录

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}

调试记录:

  1. 断点 设置的3个点用于监控字符串处理中的变换情况
    在这里插入图片描述

在这里插入图片描述
单步调试发现大写字符全变成小写
在这里插入图片描述
标点逐个去除
在这里插入图片描述字符串拆解成word list,方便统计
在这里插入图片描述
调试技术部分观察各个key的变换情况在这里插入图片描述

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值