这是阿里巴巴2016年的一道面试题:
统计英文文章中单词出现的次数,并且输出出现次数的前10个单词
文本如下:
Accessing Text from the Web and from Disk
Electronic Books
A small sample of texts from Project Gutenberg appears in the NLTK corpus collection. However, you may be interested in analyzing other texts from Project Gutenberg. You can browse the catalog of 25,000 free online books at http://www.gutenberg.org/catalog/, and obtain a URL to an ASCII text file. Although 90% of the texts in Project Gutenberg are in English, it includes material in over 50 other languages, including Catalan, Chinese, Dutch, Finnish, French, German, Italian,
先简单的看了下文章,基本单词间的分隔都是空格,或者是逗号加空格
方法一
1、先打开文档,进行初步分析
def read_file():
f=open('F:\\Python\\testfile\\test_3.txt')
readline=f.readlines()
word=[]#存储单词
#得到文章的单词并且存入列表中:
for line in readline:
#因为原文中每个单词都是用空格 或者逗号加空格分开的,
line=line.replace(