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

首頁 > 編程 > Python > 正文

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

2020-01-04 18:07:28
字體:
供稿:網(wǎng)友

這篇文章主要介紹了Python找出文件中使用率最高的漢字,涉及Python針對字符串與中文的相關(guān)操作技巧,需要的朋友可以參考下

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

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

 

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

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

 

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

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

 

 
  1. # -*- coding: gbk -*-  
  2. import codecs  
  3. from time import time  
  4. from operator import itemgetter  
  5. def top_words(filename, size=10, encoding='gbk'):  
  6. count = {}  
  7. 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']:  
  8. count[word] = 1 + count.get(word, 0)  
  9. top_words = sorted(count.iteritems(), key=itemgetter(1), reverse=True)[:size]  
  10. print '/n'.join([u'%s : %s次' % (word, times) for word, times in top_words])  
  11. begin = time()  
  12. top_words('空之境界.txt')  
  13. print '一共耗時 : %s秒' % (time()-begin) 

此外還可以引入with語句,這樣只需一行就能獲得異常安全性。

3者性能幾乎一樣,結(jié)果如下:

 

 
  1. 的 : 17533次 
  2. 是 : 8581次 
  3. 不 : 6375次 
  4. 我 : 6168次 
  5. 了 : 5586次 
  6. 一 : 5197次 
  7. 這 : 4394次 
  8. 在 : 4264次 
  9. 有 : 4188次 
  10. 人 : 4025次 
  11. 一共耗時 : 0.5秒 

引入psyco模塊的成績:

 

 
  1. 的 : 17533次 
  2. 是 : 8581次 
  3. 不 : 6375次 
  4. 我 : 6168次 
  5. 了 : 5586次 
  6. 一 : 5197次 
  7. 這 : 4394次 
  8. 在 : 4264次 
  9. 有 : 4188次 
  10. 人 : 4025次 
  11. 一共耗時 : 0.280999898911秒 

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

希望本文所述對大家的Python程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 临潭县| 吐鲁番市| 潞西市| 虎林市| 阳谷县| 青海省| 平潭县| 遂溪县| 肇东市| 读书| 铁岭市| 大冶市| 鞍山市| 怀宁县| 鸡泽县| 阳泉市| 迭部县| 禹州市| 临汾市| 尼玛县| 舒兰市| 绥滨县| 三台县| 涿州市| 松阳县| 虞城县| 芒康县| 安宁市| 潼南县| 小金县| 广安市| 资中县| 永嘉县| 威宁| 邛崃市| 东乌珠穆沁旗| 锦屏县| 八宿县| 墨脱县| 磐石市| 渭源县|