Python实现统计文本当中单词的数量,

本文介绍了一种使用Python统计英文文章中单词出现频率的方法,包括读取文件、清理和排序单词、输出出现次数最多的前10个单词。此外还提供了一个基于Spark的大数据处理方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  这是阿里巴巴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(
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值