python實現(xiàn)統(tǒng)計文本中單詞出現(xiàn)的頻率詳解
本文實例為大家分享了python統(tǒng)計文本中單詞出現(xiàn)頻率的具體代碼,供大家參考,具體內(nèi)容如下
#coding=utf-8 import os from collections import Counter sumsdata=[] for fname in os.listdir(os.getcwd()): if os.path.isfile(fname) and fname.endswith('.txt'): with open(fname,'r') as fp: data=fp.readlines() sumsdata+=[line.strip().lower() for line in data] cnt=Counter() for word in sumsdata: cnt[word]+=1 cnt=dict(cnt) for key,value in cnt.items(): print(key+":"+str(value))
首先在和程序所在路徑下創(chuàng)建幾個文本文件,我建了兩個,文件內(nèi)容分別為hello python goodbye python 和 i like python。運行程序,得到以下結(jié)果
以上所述是小編給大家介紹的python統(tǒng)計文本中單詞出現(xiàn)頻率詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
nlp計數(shù)法應用于PTB數(shù)據(jù)集示例詳解
這篇文章主要為大家介紹了nlp計數(shù)法應用于PTB數(shù)據(jù)集示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-04-04python2 中 unicode 和 str 之間的轉(zhuǎn)換及與python3 str 的區(qū)別
這篇文章主要介紹了python2 中 unicode 和 str 之間的轉(zhuǎn)換及與python3 str 的區(qū)別 本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07pyspark對Mysql數(shù)據(jù)庫進行讀寫的實現(xiàn)
這篇文章主要介紹了pyspark對Mysql數(shù)據(jù)庫進行讀寫的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12