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. 可以点下键到下一步
相关推荐
落羽凉笙1 天前
Python学习笔记(3)|数据类型、变量与运算符:夯实基础,从入门到避坑(附图解+代码)
笔记·python·学习
Quintus五等升1 天前
深度学习①|线性回归的实现
人工智能·python·深度学习·学习·机器学习·回归·线性回归
ytttr8731 天前
隐马尔可夫模型(HMM)MATLAB实现范例
开发语言·算法·matlab
天远Date Lab1 天前
Python实战:对接天远数据手机号码归属地API,实现精准用户分群与本地化运营
大数据·开发语言·python
listhi5201 天前
基于Gabor纹理特征与K-means聚类的图像分割(Matlab实现)
开发语言·matlab
哈里谢顿1 天前
Python异常链:谁才是罪魁祸首?一探"The above exception"的时间顺序
python
qq_433776421 天前
【无标题】
开发语言·php
哈里谢顿1 天前
验证 list() 会调用 `__len__` 方法的深度解析
python·django
Davina_yu1 天前
Windows 下升级 R 语言至最新版
开发语言·windows·r语言
阿珊和她的猫1 天前
IIFE:JavaScript 中的立即调用函数表达式
开发语言·javascript·状态模式