一篇英文文档中找出频数最多的10个单词
生活随笔
收集整理的這篇文章主要介紹了
一篇英文文档中找出频数最多的10个单词
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
"""
一篇英文文檔中找出頻數(shù)最多的10個(gè)單詞
collections:
Counter 提供計(jì)數(shù)器工具以支持方便和快速的計(jì)數(shù)
most_common(n) 返回n個(gè)最常見元素及其計(jì)數(shù)的列表,從最常見到最少。
如果省略nNone,則 most_common()返回計(jì)數(shù)器中的所有元素。
"""
import re
from collections import Counter
# print(dir(Counter))
with open('english.txt', 'r' ) as f:
words = f.read() # 將文件的內(nèi)容全部讀取成一個(gè)字符串
# print(re.split(r"\W+",words))
count = Counter(re.split(r"\W+", words)) # 以單詞為分隔 \w:用于匹配字母,數(shù)字或下劃線字符\W:用于匹配所有與\w不匹配的字符;
result = count.most_common(10) # 統(tǒng)計(jì)最常使用的前10個(gè)
print(result)
一篇英文文檔中找出頻數(shù)最多的10個(gè)單詞
collections:
Counter 提供計(jì)數(shù)器工具以支持方便和快速的計(jì)數(shù)
most_common(n) 返回n個(gè)最常見元素及其計(jì)數(shù)的列表,從最常見到最少。
如果省略nNone,則 most_common()返回計(jì)數(shù)器中的所有元素。
"""
import re
from collections import Counter
# print(dir(Counter))
with open('english.txt', 'r' ) as f:
words = f.read() # 將文件的內(nèi)容全部讀取成一個(gè)字符串
# print(re.split(r"\W+",words))
count = Counter(re.split(r"\W+", words)) # 以單詞為分隔 \w:用于匹配字母,數(shù)字或下劃線字符\W:用于匹配所有與\w不匹配的字符;
result = count.most_common(10) # 統(tǒng)計(jì)最常使用的前10個(gè)
print(result)
轉(zhuǎn)載于:https://www.cnblogs.com/guichao/p/11137461.html
總結(jié)
以上是生活随笔為你收集整理的一篇英文文档中找出频数最多的10个单词的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2019年年终总结(流水账)
- 下一篇: 简明python教程最新pdf_《简明P