日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python简单词频统计_python简单词频统计

發布時間:2025/3/19 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python简单词频统计_python简单词频统计 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

任務

簡單統計一個小說中哪些個漢字出現的頻率最高

知識點

文件操作

字典

排序

lambda

代碼

import codecs

import matplotlib.pyplot as plt

from pylab import mpl

mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默認字體

mpl.rcParams['axes.unicode_minus'] = False # 解決保存圖像是負號'-'顯示為方塊的問題

word = []

counter = {}

with codecs.open('data.txt') as fr:

for line in fr:

line = line.strip()

if len(line) == 0:

continue

for w in line:

if not w in word:

word.append(w)

if not w in counter:

counter[w] = 0

else:

counter[w] += 1

counter_list = sorted(counter.items(), key=lambda x: x[1], reverse=True)

print(counter_list[:50])

label = list(map(lambda x: x[0], counter_list[:50]))

value = list(map(lambda y: y[1], counter_list[:50]))

plt.bar(range(len(value)), value, tick_label=label)

plt.show()

統計了一個11M的小說,結果如下:

[(',', 288508), ('。', 261584), ('的', 188693), ('陳', 92565), ('歡', 92505), ('不', 91234), ('是', 90562), ('了', 86931), ('一', 79059), ('著', 77997), ('他'

, 71695), ('這', 63580), ('人', 61210), ('“', 59719), ('”', 59115), ('有', 56054), ('就', 52862), ('個', 49097), ('都', 46850), ('你', 45400), ('來', 42659),

('我', 40057), ('在', 37676), ('們', 36966), ('到', 36351), ('說', 35828), ('還', 35260), ('么', 32601), ('下', 31742), ('地', 30692), ('得', 29904), ('上', 2

9627), ('看', 28408), ('沒', 28333), ('出', 27937), ('道', 27732), ('大', 27012), ('?', 26729), ('那', 26589), ('要', 26076), ('子', 25035), ('自', 24012), ('

點', 23942), ('好', 21345), ('想', 21242), ('里', 20915), ('面', 20661), ('她', 20313), ('過', 20304), ('話', 20110)]

總結

以上是生活随笔為你收集整理的python简单词频统计_python简单词频统计的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。