Python讀取英文文件并記錄每個(gè)單詞出現(xiàn)次數(shù)后降序輸出示例
本文實(shí)例講述了Python讀取英文文件并記錄每個(gè)單詞出現(xiàn)次數(shù)后降序輸出。分享給大家供大家參考,具體如下:
對(duì)文中出現(xiàn)的句號(hào),逗號(hào)和感嘆號(hào)做了相應(yīng)的處理
sorted
排序函數(shù)用法:
按照value值降序排列:
sorted(dict.items(),key=lambda k:k[1],reverse=True)
按照value值升序排序:
sorted(dict.items(),key=lambda k:k[1],reverse=False)
或者
sorted(dict.items(),key=lambda k:k[1])
按照key值降序排列:
sorted(dict.items(),key=lambda k:k[0],reverse=True)
按照key值升序排列:
sorted(dict.items(),key=lambda k:k[0])
或者
sorted(dict.items(),key=lambda k:k[0],reverse=False)
Python示例:
# -*- coding:utf-8 -*- #! python2 file_object=open("english.txt") dict={} for line in file_object: line=line.replace(","," ") line=line.replace("."," ") line=line.replace("!"," ") strs= line.split(); for str in strs: if dict.has_key(str): dict[str]+=1 else: dict[str]=1 result=sorted(dict.items(),key=lambda k:k[1],reverse=True) print result
english.txt文件:
We are busy all day, like swarms of flies without souls, noisy, restless, unable to hear the voices of the soul. As time goes by, childhood away, we grew up, years away a lot of memories, once have also eroded the bottom of the childish innocence, we regardless of the shackles of mind, indulge in the world buckish, focus on the beneficial principle, we have lost themselves.
運(yùn)行結(jié)果:
[('the', 7), ('of', 6), ('we', 3), ('have', 2), ('away', 2), ('flies', 1), ('regardless', 1), ('restless', 1), ('up', 1), ('indulge', 1), ('mind', 1), ('all', 1), ('voices', 1), ('are', 1), ('in', 1), ('We', 1), ('busy', 1), ('shackles', 1), ('also', 1), ('memories', 1), ('by', 1), ('to', 1), ('unable', 1), ('goes', 1), ('themselves', 1), ('lot', 1), ('on', 1), ('buckish', 1), ('focus', 1), ('souls', 1), ('hear', 1), ('innocence', 1), ('world', 1), ('years', 1), ('day', 1), ('noisy', 1), ('a', 1), ('eroded', 1), ('grew', 1), ('like', 1), ('lost', 1), ('swarms', 1), ('bottom', 1), ('soul', 1), ('As', 1), ('without', 1), ('principle', 1), ('beneficial', 1), ('time', 1), ('childish', 1), ('childhood', 1), ('once', 1)]
PS:這里再為大家推薦2款相關(guān)統(tǒng)計(jì)工具供大家參考:
在線字?jǐn)?shù)統(tǒng)計(jì)工具:
http://tools.jb51.net/code/zishutongji
在線字符統(tǒng)計(jì)與編輯工具:
http://tools.jb51.net/code/char_tongji
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python處理multipart/form-data的請(qǐng)求方法
今天小編就為大家分享一篇python處理multipart/form-data的請(qǐng)求方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12pyqt5與matplotlib的完美結(jié)合實(shí)例
今天小編就為大家分享一篇pyqt5與matplotlib的完美結(jié)合實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06python實(shí)現(xiàn)多線程網(wǎng)頁(yè)下載器
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)一個(gè)多線程網(wǎng)頁(yè)下載器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Django migrations 默認(rèn)目錄修改的方法教程
這篇文章主要介紹了Django migrations 默認(rèn)目錄修改的方法教程,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Python使用matplotlib繪制三維參數(shù)曲線操作示例
這篇文章主要介紹了Python使用matplotlib繪制三維參數(shù)曲線操作,結(jié)合實(shí)例形式分析了Python使用matplotlib的數(shù)值計(jì)算與圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2019-09-09