python從文本文件中選出使用頻率最多的單詞
2018-07-20 來(lái)源:open-open

Python語(yǔ)言: python.從一個(gè)文本文件中選出使用頻率最多的若干個(gè)單詞 #coding:utf-8 # ------------------------------------------------------------ # 簡(jiǎn)介 : 從一個(gè)文件中選出使用頻率最多的10個(gè)單詞 # 請(qǐng)自行準(zhǔn)備一個(gè) test.txt 文檔放在與本腳本相同目錄 # ------------------------------------------------------------ from time import time from operator import itemgetter def test(): # 取 10 個(gè),有需要可以修改, 及定義讀取的文件 test.txt iList = 10 strFileName = 'test.txt' count = {} for word in open(strFileName).read().split(): if count.has_key(word): count[word] = count[word] + 1 else: count[word] = 1 print sorted(count.iteritems( ), key=itemgetter(1), reverse=True)[0:iList] # 調(diào)用 if __name__ == '__main__': t1 = time() test() print time()-t1
標(biāo)簽: 腳本
版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。
最新資訊
熱門(mén)推薦