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. 可以点下键到下一步
相关推荐
长安第一美人3 分钟前
算能 BM1688 低延迟推流:Qt+WebSocket 直出 H5/HDMI
开发语言·网络·嵌入式硬件·websocket·交互
LiAo_1996_Y4 分钟前
MongoDB中什么是Hashed Shard Key的哈希冲突_哈希函数的分布均匀性分析
jvm·数据库·python
lhbian12 分钟前
C++、C与易语言:编程语言对比解析
c语言·开发语言·c++
꧁细听勿语情꧂12 分钟前
数据结构概念和算法、时间复杂度、空间复杂度引入
c语言·开发语言·数据结构·算法
weixin_3812881812 分钟前
PyTorch中高效实现SOM邻域权重更新:向量化替代双重循环
jvm·数据库·python
Yuanxl90317 分钟前
pytorch-优化器
人工智能·pytorch·python
m0_6178814219 分钟前
在 Go 中声明包级全局 Map 的正确方法
jvm·数据库·python
Polar__Star20 分钟前
Redis怎样管理废弃的数据集合_利用EXPIRE指令为任意数据类型设置生命周期
jvm·数据库·python
weixin_5689960620 分钟前
CSS布局如何解决父级因全是绝对定位导致本身没高度的问题
jvm·数据库·python
weixin_3812881820 分钟前
MySQL无法通过网络连接服务器_检查bind-address与访问权限
jvm·数据库·python