Python 基础知识

Python实现wordcount

复制代码
import re
from collections import defaultdict

def wordcount(text):
    words = re.findall(r'\b\w+\b', text.lower())
    
    word_count = defaultdict(int)
    for word in words:
        word_count[word] += 1
    
    return dict(word_count)

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

result = wordcount(text)
print(result)

debug

debug笔记

复制代码
1. 需要下载按照扩展插件
2. 需要创建配置文件
3. 需要在代码左边点个点
4. 可以点下键到下一步
相关推荐
沐知全栈开发8 分钟前
Bootstrap 多媒体对象
开发语言
PythonFun12 分钟前
WPS动态序号填充,告别手动调整烦恼
java·前端·python
Hx_Ma1613 分钟前
测试题(二)
java·开发语言
ShineWinsu13 分钟前
对于C++中list的详细介绍
开发语言·数据结构·c++·算法·面试·stl·list
tackchen13 分钟前
venv-manager 管理 Conda 环境 和 Python 虚拟环境 (venv)
开发语言·python·conda
py小王子21 分钟前
GitHub 文件/文件夹批量上传工具
python·github
霸王•吕布22 分钟前
C#-使用OpenTK渲染3D模型
c#·opentk·glcontrol
lly20240623 分钟前
ASP #include 指令详解
开发语言
2501_9369603623 分钟前
ROS快速入门杰哥教程1-77残缺版
java·开发语言
游乐码26 分钟前
c#封装成员变量和成员方法和访问修饰符
开发语言·c#