Python获取一段文章中字母出现频率前5的字母以及个数(去除空格、换行符等,只算字母)

本文对比了三种在Python中统计字符串中字符频率的方法,包括传统字典计数、使用Counter库以及查找最大值的方法。实验结果显示,使用Counter库的方法效率最高。

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

import time,re
from collections import Counter


text = 'A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.'
result = re.findall('[a-zA-Z]', text.lower())

t1 = time.time()
for i in range(0,1000):
	dict = {}
	s = set(result) # 集合:得到不重复的元素
	for i in s:
	    dict[i] = result.count(i) # 对集合中每个元素分别计数,存入dictionary中
	testlist = sorted(dict.items(),key = lambda x:x[1],reverse = True)
t2 = time.time()
print(testlist[0:5])
print ("first method:",t2-t1)


t1 = time.time()
for i in range(0,1000):
	count = Counter(result)
	testlist = sorted(count.items(),key = lambda x:x[1],reverse = True)
t2 = time.time()
print(testlist[0:5])
print("second method:",t2-t1)

执行结果:

[('e', 760), ('i', 640), ('a', 640), ('n', 560), ('r', 520)]
first method: 6.3350348472595215
[('e', 760), ('i', 640), ('a', 640), ('n', 560), ('r', 520)]
second method: 0.7251110076904297

显然第二种使用Counter的方法速度更快!

另外用max试了一下返回出现次数最多的

t1 = time.time()
for i in range(0,10):
	test=max(result, key=result.count)
t2 = time.time()
print(test)
print ("third method:",t2-t1)

结果:

e
third method: 21.224278211593628

运行10次需要21秒多,运行一次大概需要2秒,比较慢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值