国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python找出文件中使用率最高的漢字實例詳解

2020-02-23 01:30:25
字體:
來源:轉載
供稿:網友

本文實例講述了Python找出文件中使用率最高的漢字的方法。分享給大家供大家參考。具體分析如下:

這是我初學Python時寫的,為了簡便,我并沒在排序完后再去掉非中文字符,稍微會影響性能(大約增加了25%的時間)。

# -*- coding: gbk -*- import codecs from time import time from operator import itemgetter def top_words(filename, size=10, encoding='gbk'):   count = {}   for line in codecs.open(filename, 'r', encoding):     for word in line:       if u'/u4E00' <= word <= u'/u9FA5' or u'/uF900' <= word <= u'/uFA2D':         count[word] = 1 + count.get(word, 0)   top_words = sorted(count.iteritems(), key=itemgetter(1), reverse=True)[:size]   print '/n'.join([u'%s : %s次' % (word, times) for word, times in top_words]) begin = time() top_words('空之境界.txt') print '一共耗時 : %s秒' % (time()-begin) 

如果想用上新方法,以及讓join的可讀性更高的話,這樣也是可以的:

# -*- coding: gbk -*- import codecs from time import time from operator import itemgetter from heapq import nlargest def top_words(filename, size=10, encoding='gbk'):   count = {}   for line in codecs.open(filename, 'r', encoding):     for word in line:       if u'/u4E00' <= word <= u'/u9FA5' or u'/uF900' <= word <= u'/uFA2D':         count[word] = 1 + count.get(word, 0)   top_words = nlargest(size, count.iteritems(), key=itemgetter(1))   for word, times in top_words:     print u'%s : %s次' % (word, times) begin = time() top_words('空之境界.txt') print '一共耗時 : %s秒' % (time()-begin) 

或者讓行數更少(好囧的列表綜合):

# -*- coding: gbk -*- import codecs from time import time from operator import itemgetter def top_words(filename, size=10, encoding='gbk'):   count = {}   for word in [word for word in codecs.open(filename, 'r', encoding).read() if u'/u4E00' <= word <= u'/u9FA5' or u'/uF900' <= word <= u'/uFA2D']:     count[word] = 1 + count.get(word, 0)   top_words = sorted(count.iteritems(), key=itemgetter(1), reverse=True)[:size]   print '/n'.join([u'%s : %s次' % (word, times) for word, times in top_words]) begin = time() top_words('空之境界.txt') print '一共耗時 : %s秒' % (time()-begin) 

此外還可以引入with語句,這樣只需一行就能獲得異常安全性。
3者性能幾乎一樣,結果如下:

的 : 17533次是 : 8581次不 : 6375次我 : 6168次了 : 5586次一 : 5197次這 : 4394次在 : 4264次有 : 4188次人 : 4025次一共耗時 : 0.5秒

引入psyco模塊的成績:

的 : 17533次是 : 8581次不 : 6375次我 : 6168次了 : 5586次一 : 5197次這 : 4394次在 : 4264次有 : 4188次人 : 4025次一共耗時 : 0.280999898911秒

 

注:測試文件為778KB的GBK編碼,40余萬字。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 山丹县| 洛宁县| 秭归县| 祁东县| 蓬溪县| 邻水| 阿拉善盟| 敖汉旗| 罗山县| 平顶山市| 普格县| 绥宁县| 抚松县| 新兴县| 星子县| 太原市| 青川县| 河北省| 翁源县| 镇康县| 望奎县| 自贡市| 互助| 德钦县| 临沧市| 麻江县| 离岛区| 涞源县| 都兰县| 洱源县| 浦城县| 北票市| 临夏县| 宁远县| 庆安县| 柏乡县| 扶余县| 绥滨县| 修武县| 丹江口市| 彭山县|