python lcut()函数_Python学习的第三天

1. 词云WordCloud——续

①Python中使用open内置函数进行文件读取

②利用函数jieba.lcut(words)进行分词

③过滤重复词和无关词

④给十个人物出现的次数进行排序

⑤输出图片

示例一:三国TOP10人物分析

import jieba

from wordcloud import WordCloud

# 1.读取小说内容

with open('./novel/threekingdom.txt','r',encoding='utf-8') as f:

words = f.read()

counts = {} #{‘曹操’:234,‘回寨’:56}

excludes = {"将军", "却说", "丞相", "二人", "不可", "荆州", "不能", "如此", "商议",

"如何", "主公", "军士", "军马", "左右", "次日", "引兵", "大喜", "天下",

"东吴", "于是", "今日", "不敢", "魏兵", "陛下", "都督", "人马", "不知",

"孔明曰","玄德曰","刘备","云长"}

# 2.分词

words_list = jieba.lcut(words)

print(words_list)

for word in words_list:

if len(word) <= 1:

continue

else:

# 更新字典中的值

# counts[word] = 去除字典中原来键相应的值 + 1

# counts[word] = counts[word] + 1 # counts[words]如果没有就要报错

# 字典。get(k) 如果字典中没有这个键,返回NONE

counts[word] = counts.get(word, 0) + 1

print(counts)

# 3.词语过滤,删除无关词,重复词

counts['孔明'] = counts['孔明'] + counts['孔明曰']

counts['玄德'] = counts['玄德'] + counts['玄德曰'] + counts['刘备']

counts['关公'] = counts['关公'] + counts['云长']

for word in excludes:

del counts[word]

# 4.排序[(),()]

items = list(counts.items())

print(items)

def sort_by_count(x):

return x[1]

# items.sort(key=sort_by_count,reverse=True)

items.sort(key=lambda i:i[1],reverse=True)

li = [] #['孔明','','']

# 遍历

for i in range(10):

# 序列解包

role, c

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值