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. 可以点下键到下一步
相关推荐
naruto_lnq9 小时前
泛型编程与STL设计思想
开发语言·c++·算法
2301_821369619 小时前
用Python生成艺术:分形与算法绘图
jvm·数据库·python
:Concerto9 小时前
JavaSE 注解
java·开发语言·sprint
机 _ 长9 小时前
YOLO26 改进 | 基于特征蒸馏 | 知识蒸馏 (Response & Feature-based Distillation)
python·深度学习·机器学习
m0_7487080510 小时前
C++中的观察者模式实战
开发语言·c++·算法
qq_5375626710 小时前
跨语言调用C++接口
开发语言·c++·算法
wjs202410 小时前
DOM CDATA
开发语言
Tingjct10 小时前
【初阶数据结构-二叉树】
c语言·开发语言·数据结构·算法
2401_8321319510 小时前
Python单元测试(unittest)实战指南
jvm·数据库·python
猷咪10 小时前
C++基础
开发语言·c++