python統(tǒng)計(jì)文本字符串里單詞出現(xiàn)頻率的方法
本文實(shí)例講述了python統(tǒng)計(jì)文本字符串里單詞出現(xiàn)頻率的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
# word frequency in a text # tested with Python24 vegaseat 25aug2005 # Chinese wisdom ... str1 = """Man who run in front of car, get tired. Man who run behind car, get exhausted.""" print "Original string:" print str1 print # create a list of words separated at whitespaces wordList1 = str1.split(None) # strip any punctuation marks and build modified word list # start with an empty list wordList2 = [] for word1 in wordList1: # last character of each word lastchar = word1[-1:] # use a list of punctuation marks if lastchar in [",", ".", "!", "?", ";"]: word2 = word1.rstrip(lastchar) else: word2 = word1 # build a wordList of lower case modified words wordList2.append(word2.lower()) print "Word list created from modified string:" print wordList2 print # create a wordfrequency dictionary # start with an empty dictionary freqD2 = {} for word2 in wordList2: freqD2[word2] = freqD2.get(word2, 0) + 1 # create a list of keys and sort the list # all words are lower case already keyList = freqD2.keys() keyList.sort() print "Frequency of each word in the word list (sorted):" for key2 in keyList: print "%-10s %d" % (key2, freqD2[key2])
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
- python統(tǒng)計(jì)字符串中指定字符出現(xiàn)次數(shù)的方法
- Python實(shí)現(xiàn)統(tǒng)計(jì)英文單詞個(gè)數(shù)及字符串分割代碼
- python實(shí)現(xiàn)字符串中字符分類及個(gè)數(shù)統(tǒng)計(jì)
- Python統(tǒng)計(jì)一個(gè)字符串中每個(gè)字符出現(xiàn)了多少次的方法【字符串轉(zhuǎn)換為列表再統(tǒng)計(jì)】
- python統(tǒng)計(jì)字符串中字母出現(xiàn)次數(shù)代碼實(shí)例
- python 統(tǒng)計(jì)文件中的字符串?dāng)?shù)目示例
- Python統(tǒng)計(jì)字符串中英文字母、空格、數(shù)字和其它字符的個(gè)數(shù)
相關(guān)文章
python3實(shí)現(xiàn)raspberry pi(樹莓派)4驅(qū)小車控制程序
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)raspberry pi(樹莓派)4驅(qū)小車控制程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02windows10下python3.5 pip3安裝圖文教程
這篇文章主要為大家詳細(xì)介紹了windows10下python3.5 pip3安裝圖文教程,注意區(qū)分python 2.x和python 3.x的相關(guān)命令,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Python應(yīng)用之利用pyecharts畫中國(guó)地圖
這篇文章主要介紹了Python應(yīng)用之利用pyecharts畫中國(guó)地圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07python3跳出一個(gè)循環(huán)的實(shí)例操作
在本篇內(nèi)容里小編給大家整理的是關(guān)于python3跳出一個(gè)循環(huán)的實(shí)例操作內(nèi)容,有需要的朋友們可以參考下。2020-08-08python中的List sort()與torch.sort()
這篇文章主要介紹了python中的List sort()與torch.sort()使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09基于Python實(shí)現(xiàn)大文件分割和命名腳本過程解析
這篇文章主要介紹了基于Python實(shí)現(xiàn)大文件分割和命名腳本過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09python3利用tcp實(shí)現(xiàn)文件夾遠(yuǎn)程傳輸
這篇文章主要為大家詳細(xì)介紹了python3利用tcp實(shí)現(xiàn)文件夾遠(yuǎn)程傳輸,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07