【书生大模型实战】L0-Python

一、关卡任务

任务类型任务内容预计耗时
闯关任务Python实现wordcount15mins
闯关任务Vscode连接InternStudio debug笔记15mins

二、实验过程

2.1 Python实现wordcount

import re
from collections import defaultdict

def wordcount(text):
    # 去掉标点符号
    text = re.sub(r'[^\w\s]', '', text)
    # 转换成小写
    text = text.lower()
    # 拆分成单词
    words = text.split()
    
    # 使用defaultdict统计单词出现次数
    word_count = defaultdict(int)
    for word in words:
        word_count[word] += 1
    
    return dict(word_count)

# 示例输入
text = """
Hello world!
This is an example.
Word count is fun.
Is it fun to count words?
Yes, it is fun!
"""

# 调用函数并输出结果
result = wordcount(text)
print(result)

2.2 Vscode连接InternStudio debug笔记

2.2.1 显示text变量内容

2.2.2 去掉标点

2.2.3 变成小写

2.2.4 拆分成单词

2.2.5 使用defaultdict统计单词出现次数

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值