欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年06月28日 14:33:20   作者:菜鳥蟲師  
這篇文章主要介紹了Python讀取英文文件并記錄每個單詞出現(xiàn)次數(shù)后降序輸出,涉及Python文件讀取、字符串替換、分割以及字典遍歷、排序等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了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 -*-
#! 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.

運行結(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)典教程

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

相關(guān)文章

  • Python如何在DataFrame增加數(shù)值

    Python如何在DataFrame增加數(shù)值

    這篇文章主要介紹了Python如何在DataFrame增加數(shù)值,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • python清除字符串中間空格的實例講解

    python清除字符串中間空格的實例講解

    今天小編就為大家分享一篇python清除字符串中間空格的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • python處理multipart/form-data的請求方法

    python處理multipart/form-data的請求方法

    今天小編就為大家分享一篇python處理multipart/form-data的請求方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • 一篇文章弄懂Python中的內(nèi)建函數(shù)

    一篇文章弄懂Python中的內(nèi)建函數(shù)

    Python學(xué)習(xí),內(nèi)建函數(shù)是你必須要掌握的一部分,下面這篇文章主要給大家介紹了關(guān)于Python中內(nèi)建函數(shù)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2021-08-08
  • 解決Pycharm 導(dǎo)入其他文件夾源碼的2種方法

    解決Pycharm 導(dǎo)入其他文件夾源碼的2種方法

    今天小編就為大家分享一篇解決Pycharm 導(dǎo)入其他文件夾源碼的2種方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • pyqt5與matplotlib的完美結(jié)合實例

    pyqt5與matplotlib的完美結(jié)合實例

    今天小編就為大家分享一篇pyqt5與matplotlib的完美結(jié)合實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Python 編程語言詳細(xì)介紹

    Python 編程語言詳細(xì)介紹

    這篇文章主要分享的是 Python 編程語言的詳細(xì)介紹,Python 由 Guido van Rossum 設(shè)計,作為“ABC”編程語言的繼承者,于 1991 年首次發(fā)布。它是一種高級通用語言,其設(shè)計理念是通過使用縮進(jìn)來強調(diào)代碼的可讀性。文章具有一定的參考價值,需要的朋友可以參考一下
    2021-11-11
  • python實現(xiàn)多線程網(wǎng)頁下載器

    python實現(xiàn)多線程網(wǎng)頁下載器

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)一個多線程網(wǎng)頁下載器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Django migrations 默認(rèn)目錄修改的方法教程

    Django migrations 默認(rèn)目錄修改的方法教程

    這篇文章主要介紹了Django migrations 默認(rèn)目錄修改的方法教程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • Python使用matplotlib繪制三維參數(shù)曲線操作示例

    Python使用matplotlib繪制三維參數(shù)曲線操作示例

    這篇文章主要介紹了Python使用matplotlib繪制三維參數(shù)曲線操作,結(jié)合實例形式分析了Python使用matplotlib的數(shù)值計算與圖形繪制相關(guān)操作技巧,需要的朋友可以參考下
    2019-09-09

最新評論