书生浦语大模型实战营---Python task

任务一

请实现一个wordcount函数,统计英文字符串中每个单词出现的次数,通过构建defaultdict字典,可以避免插入值时需要判断值是否存在

from collections import defaultdict

def word_count(text):
    #构建缓存
    reval = defaultdict(int)
    words = text.strip().split()
    for word in words:
        # 清洗单词,移除标点符号等
        cleaned_word = ''.join(char.lower() for char in word if char.isalnum())
        reval[cleaned_word]+=1

    return reval

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.
"""

print(word_count(text))

输出结果
在这里插入图片描述

任务二

首先是进入函数内,目前缓存中有一个局部变量
在这里插入图片描述
首先对文本进行split,
在这里插入图片描述
执行完函数后
在这里插入图片描述

相关推荐

  1. 书生·模型实战】学习笔记目录

    2024-07-13 09:48:04       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 09:48:04       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 09:48:04       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 09:48:04       57 阅读
  4. Python语言-面向对象

    2024-07-13 09:48:04       68 阅读

热门阅读

  1. Redis原子计数器incr,防止并发请求

    2024-07-13 09:48:04       25 阅读
  2. 求某个矩阵的鞍点的个数

    2024-07-13 09:48:04       20 阅读
  3. 13 IP层协议-网际控制报文协议ICMP

    2024-07-13 09:48:04       25 阅读
  4. Electron31.x+vite5+vue3 setup客户端Exe聊天系统演示

    2024-07-13 09:48:04       22 阅读
  5. 远程调试Xcode:一键解锁iOS开发新境界

    2024-07-13 09:48:04       27 阅读
  6. oracle 表空间文件迁移

    2024-07-13 09:48:04       26 阅读
  7. xml详解

    xml详解

    2024-07-13 09:48:04      33 阅读
  8. 【软件测试】 1+X中级 自动化测试试题

    2024-07-13 09:48:04       23 阅读
  9. PostgreSQL UPDATE 命令

    2024-07-13 09:48:04       19 阅读
  10. 手撕排序算法:选择排序

    2024-07-13 09:48:04       28 阅读