python词频统计完整步骤_python简单词频统计

词频统计

简单英语词频统计

# 词频:单词出现的次数

f = open(r'D:\上海Python11期视频\预科班\hamlet.txt','r',encoding='utf8')

data = f.read().lower()

# print(data)

data_split = data.split(' ')

# print(data_split)

count_dict = {}

for word in data_split:

if word not in count_dict:

count_dict[word] = 1

else:

count_dict[word] += 1

# print(count_dict)

def func(i):

return i[1]

lt = list(count_dict.items())

lt.sort(key=func)

lt.reverse()

for i in lt[0:10]:

print(f'{i[0]:^7}{i[1]^5}')

1739658-20190719144824632-1923059210.png

简单中文词频统计

import jieba

f = open(r'F:\pythonxm\7.19day06\threekingdoms.txt','r',encoding='utf8')

data = f.read()

data_jieba = jieba.lcut(data)

count_dict = {}

for word in data_jieba :

if len(word) == 1 :

continue

if word in {"次日","引兵","军马","左右","将军", "却说", "荆州", "二人","如何","军士","不可", "不能", "如此", "商议"}:

continue

if '曰' in word :

word = word.replace('曰','')

if word == '玄德' :

word = '刘备'

# if word == '孔明曰':

# word = '孔明'

# elif word == '玄德曰':

# word = '玄德'

if word in count_dict :

count_dict[word] += 1

else:

count_dict[word] = 1

def func(i):

return i[1]

data_list = list(count_dict.items())

data_list.sort(key=func)

data_list.reverse()

for i in data_list[0:10] :

print(f'{i[0]:^4}{i[1] ^ 6}')

1739658-20190719144854245-1164565500.png

作  者:豆瓣酱瓣豆

出  处:https://www.cnblogs.com/chenziqing/

查看其它博文请点击:https://www.cnblogs.com/chenziqing/

声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!

微信:

1739658-20190714095725158-117902979.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值