Python找出文件中使用率最高的漢字實(shí)例詳解
本文實(shí)例講述了Python找出文件中使用率最高的漢字的方法。分享給大家供大家參考。具體分析如下:
這是我初學(xué)Python時(shí)寫的,為了簡便,我并沒在排序完后再去掉非中文字符,稍微會(huì)影響性能(大約增加了25%的時(shí)間)。
# -*- 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 '一共耗時(shí) : %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 '一共耗時(shí) : %s秒' % (time()-begin)
或者讓行數(shù)更少(好囧的列表綜合):
# -*- 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 '一共耗時(shí) : %s秒' % (time()-begin)
此外還可以引入with語句,這樣只需一行就能獲得異常安全性。
3者性能幾乎一樣,結(jié)果如下:
的 : 17533次 是 : 8581次 不 : 6375次 我 : 6168次 了 : 5586次 一 : 5197次 這 : 4394次 在 : 4264次 有 : 4188次 人 : 4025次 一共耗時(shí) : 0.5秒
引入psyco模塊的成績:
的 : 17533次 是 : 8581次 不 : 6375次 我 : 6168次 了 : 5586次 一 : 5197次 這 : 4394次 在 : 4264次 有 : 4188次 人 : 4025次 一共耗時(shí) : 0.280999898911秒
注:測試文件為778KB的GBK編碼,40余萬字。
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
詳解使用python3.7配置開發(fā)釘釘群自定義機(jī)器人(2020年新版攻略)
這篇文章主要介紹了詳解使用python3.7配置開發(fā)釘釘群自定義機(jī)器人(2020年新版攻略),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Python中functools模塊的常用函數(shù)解析
這篇文章主要介紹了Python中functools模塊的常用函數(shù)解析,分別講解了partial、update_wrapper、wraps、total_ordering的用法,需要的朋友可以參考下2016-06-06手把手教你jupyter?notebook更換環(huán)境的方法
在日常使用jupyter-notebook時(shí),可能會(huì)碰到需要切換不同虛擬環(huán)境的場景,下面這篇文章主要給大家介紹了關(guān)于jupyter?notebook更換環(huán)境的方法,需要的朋友可以參考下2023-05-05cv2.getStructuringElement()函數(shù)及開、閉、腐蝕、膨脹原理講解
getStructuringElement()函數(shù)可用于構(gòu)造一個(gè)特定大小和形狀的結(jié)構(gòu)元素,用于圖像形態(tài)學(xué)處理,這篇文章主要介紹了cv2.getStructuringElement()函數(shù)及開、閉、腐蝕、膨脹原理講解的相關(guān)資料,需要的朋友可以參考下2022-12-12如何測試Python網(wǎng)站的訪問速度,并且優(yōu)化Python網(wǎng)站的性能
本文使用網(wǎng)絡(luò)工具和Python測速庫進(jìn)行測試Python網(wǎng)站的訪問速度,通過優(yōu)化代碼性能和優(yōu)化服務(wù)器性能以及優(yōu)化數(shù)據(jù)庫性能等有針對性地優(yōu)化Python網(wǎng)站的性能2024-01-01使用python的pyplot繪制函數(shù)實(shí)例
今天小編就為大家分享一篇使用python的pyplot繪制函數(shù)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02python主動(dòng)拋出異常raise的方法實(shí)現(xiàn)
本文主要介紹了python主動(dòng)拋出異常raise的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12Python+Selenium定位不到元素常見原因及解決辦法(報(bào):NoSuchElementException)
這篇文章主要介紹了Python+Selenium定位不到元素常見原因及解決辦法(報(bào):NoSuchElementException),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03