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. 可以点下键到下一步
相关推荐
一只会写代码的猫15 小时前
面向高性能计算与网络服务的C++微内核架构设计与多线程优化实践探索与经验分享
java·开发语言·jvm
是小胡嘛17 小时前
C++之Any类的模拟实现
linux·开发语言·c++
csbysj202018 小时前
Vue.js 混入:深入理解与最佳实践
开发语言
笨笨聊运维19 小时前
CentOS官方不维护版本,配置python升级方法,无损版
linux·python·centos
Gerardisite19 小时前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php
Want59519 小时前
C/C++跳动的爱心①
c语言·开发语言·c++
小毛驴85020 小时前
软件设计模式-装饰器模式
python·设计模式·装饰器模式
coderxiaohan20 小时前
【C++】多态
开发语言·c++
gfdhy20 小时前
【c++】哈希算法深度解析:实现、核心作用与工业级应用
c语言·开发语言·c++·算法·密码学·哈希算法·哈希