python 统计全唐诗春夏秋冬出现次数

2021/4/25 12:25:36

本文主要是介绍python 统计全唐诗春夏秋冬出现次数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

import jieba
import matplotlib.pyplot as plt
# _*_ coding:utf-8 _*_
txt=open("tangshi.txt","r",encoding="utf-8").read()
words=jieba.lcut(txt)
counts={}

for word in words:
    if word in ["春","夏","秋","冬"]:
        counts[word]=counts.get(word,0)+1;
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
bar_count={}
for i in range(4):
    word,count =items[i]
    bar_count[i]=count
    print("{0:<5}{1:>5}".format(word,count))
# 这两行代码解决 plt 中文显示的问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
season=["春","夏","秋","冬"]

plt.bar(season, bar_count)
plt.show()
# 春     1785
# 秋     1039
# 夏      114
# 冬       44




这篇关于python 统计全唐诗春夏秋冬出现次数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程