词频统计实例

本文通过一个具体的例子展示了如何使用Python进行词频统计,包括去除标点符号、将文本分割成单词列表、统计每个单词出现的次数并按频率排序。

实例:词频统计

#词频统计
import pprint
text = """
The Python Software Foundation (PSF) is a 501(c)(3) non-profit corporation that holds the intellectual 
property rights behind the Python programming language. We manage the open source licensing for Python 
version 2.1 and later and own and protect the trademarks associated with Python. We also run the North 
American PyCon conference annually, support other Python conferences around the world, and fund Python 
related development with our grants program and by funding special projects. 
"""
punctuation = "n~@#$%^&*()_-+=<>?/,.:;{}[]|\'\""  # 标点符号
for i in punctuation:
        if i in text:
            text.replace(i,' ')#去除标点符号
text_list = text.split(' ')#空格分开
text_list = [x.strip() for x in text_list]#去除换行符
number = set(text_list)#去重,准换为集合
word_number = {}
for word in number:
    word_number[word] = text_list.count(word)#计数,字典对应起来
sort_list = sorted(word_number.items(),key=lambda x:x[1],reverse=True)#排序
pprint.pprint(sort_list)

//输出
[('the', 6),
 ('Python', 5),
 ('and', 5),
 ('with', 2),
 ('We', 2),
 ('', 1),
 ('trademarks', 1),
 ('other', 1),
 ('program', 1),
 ('run', 1),
 ('is', 1),
 ('behind', 1),
 ('for', 1),
 ('protect', 1),
 ('Foundation', 1),
 ('also', 1),
 ('intellectual', 1),
 ('fund', 1),
 ('Python.', 1),
 ('our', 1),
 ('by', 1),
 ('special', 1),
 ('North', 1),
 ('conference', 1),
 ('that', 1),
 ('annually,', 1),
 ('manage', 1),
 ('programming', 1),
 ('version', 1),
 ('American', 1),
 ('own', 1),
 ('related', 1),
 ('Software', 1),
 ('source', 1),
 ('conferences', 1),
 ('property', 1),
 ('holds', 1),
 ('licensing', 1),
 ('501(c)(3)', 1),
 ('development', 1),
 ('later', 1),
 ('(PSF)', 1),
 ('associated', 1),
 ('The', 1),
 ('world,', 1),
 ('funding', 1),
 ('2.1', 1),
 ('projects.', 1),
 ('a', 1),
 ('rights', 1),
 ('language.', 1),
 ('corporation', 1),
 ('grants', 1),
 ('non-profit', 1),
 ('open', 1),
 ('PyCon', 1),
 ('support', 1),
 ('around', 1)]

进程已结束,退出代码0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kilig*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值