国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 尼勒克县| 巩义市| 二手房| 敦煌市| 策勒县| 九龙坡区| 界首市| 东阳市| 内江市| 邢台市| 绥滨县| 苍山县| 新营市| 来宾市| 祁连县| 五华县| 凯里市| 灵山县| 佛山市| 永定县| 新宾| 颍上县| 中西区| 嘉兴市| 日喀则市| 临颍县| 松桃| 奉化市| 长乐市| 东安县| 汝南县| 凤山县| 罗源县| 锡林浩特市| 兴海县| 郯城县| 白朗县| 郓城县| 姚安县| 彝良县| 鄂托克前旗|