python单词个数_python 统计单词个数
根據(jù)一篇英文文章統(tǒng)計(jì)其中單詞出現(xiàn)最多的10個(gè)單詞。
# -*- coding: utf-8 -*-
import urllib2
import re
from collections import Counter
'''
007之雷霆谷 You Only Live Twice',可以從http://novel.tingroom.com/jingdian/1584/47084.html這個(gè)地址獲取,
列出其中使用最頻繁的10個(gè)單詞,并給出它們的出現(xiàn)次數(shù)
Python2.7上測(cè)試通過
'''
'''根據(jù)URL網(wǎng)址讀取數(shù)據(jù)'''
def Get_Data(url):
data = urllib2.urlopen(url).read()
return data
'''統(tǒng)計(jì)單詞及個(gè)數(shù),text是要統(tǒng)計(jì)的文章字符串,n是統(tǒng)計(jì)次數(shù)最多的前幾個(gè)'''
def PrintWordsCount(text,n=1):
'''調(diào)用Counter用正則進(jìn)行拆分'''
wordCountList = Counter(re.split(r'\W+', text, flags=re.M|re.I)).most_common(n)
print '單詞\t次數(shù)'
print '\n'.join([w+'\t'+str(c) for w,c in wordCountList])
#測(cè)試代碼
def test():
url ='http://novel.tingroom.com/jingdian/1584/47084.html'
data = Get_Data(url)
PrintWordsCount(data,10)
test()
總結(jié)
以上是生活随笔為你收集整理的python单词个数_python 统计单词个数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【PYTHON,WORD】1.利用pyt
- 下一篇: 通达信接口python的安装方法