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

首頁 > 編程 > Python > 正文

Python讀取英文文件并記錄每個單詞出現(xiàn)次數(shù)后降序輸出示例

2020-02-15 22:04:46
字體:
供稿:網(wǎng)友

本文實例講述了Python讀取英文文件并記錄每個單詞出現(xiàn)次數(shù)后降序輸出。分享給大家供大家參考,具體如下:

對文中出現(xiàn)的句號,逗號和感嘆號做了相應(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 -*-#! python2file_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]=1result=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.

運行結(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ǐn)?shù)統(tǒng)計工具:
http://tools.jb51.net/code/zishutongji

在線字符統(tǒng)計與編輯工具:
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)典教程》

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 康保县| 长海县| 尚志市| 开封县| 增城市| 和林格尔县| 兰西县| 岑溪市| 响水县| 德昌县| 轮台县| 平谷区| 横山县| 海丰县| 通化县| 离岛区| 磐安县| 浏阳市| 保德县| 噶尔县| 嘉鱼县| 昂仁县| 喀什市| 固原市| 磐石市| 孟津县| 友谊县| 建始县| 灌南县| 海伦市| 乃东县| 渭南市| 丽水市| 富民县| 辉县市| 蒲城县| 钟山县| 阿克苏市| 东海县| 山西省| 雅安市|