gpt4 book ai didi

python - python中的字母频率

转载 作者:行者123 更新时间:2023-11-28 23:06:19 25 4
gpt4 key购买 nike

我需要编写一个程序来打印文本中字母出现的频率文件并将该频率与 python 中的另一个频率进行比较。

到目前为止,我能够打印字母出现的次数,但是我得到的百分比频率是错误的。我认为这是因为我需要我的程序只计算通过删除所有空格和其他文件中的字母数人物。

def addLetter (x):
result = ord(x) - ord(a)
return result


#start of the main program
#prompt user for a file

while True:
speech = raw_input("Enter file name:")

wholeFile = open(speech, 'r+').read()
lowlet = wholeFile.lower()
letters= list(lowlet)
alpha = list('abcdefghijklmnopqrstuvwxyz')
n = len(letters)
f = float(n)
occurrences = {}
d = {}


#number of letters
for x in alpha:
occurrences[x] = letters.count(x)
d[x] =(occurrences[x])/f
for x in occurrences:
print x, occurrences[x], d[x]

这是输出

Enter file name:dems.txt
a 993 0.0687863674148
c 350 0.0242449431976
b 174 0.0120532003325
e 1406 0.0973954003879
d 430 0.0297866444999
g 219 0.015170407315
f 212 0.0146855084511
i 754 0.0522305347742
h 594 0.0411471321696
k 81 0.00561097256858
j 12 0.000831255195345
m 273 0.0189110556941
l 442 0.0306178996952
o 885 0.0613050706567
n 810 0.0561097256858
q 9 0.000623441396509
p 215 0.0148933222499
s 672 0.0465502909393
r 637 0.0441257966196
u 305 0.021127736215
t 1175 0.0813937378775
w 334 0.0231366029371
v 104 0.00720421169299
y 212 0.0146855084511
x 13 0.000900526461624
z 6 0.000415627597672
Enter file name:

该程序确实按列打印,但我不太确定如何在此处显示它。

“a”的频率应该是 .0878

最佳答案

您可以使用 translator recipe删除所有不在 alpha 中的字符。由于这样做会使 letters 仅包含来自 alpha 的字符,因此 n 现在是正确的分母。

然后您可以使用 collections.defaultdict(int) 来计算字母的出现次数:

import collections
import string

def translator(frm='', to='', delete='', keep=None):
# Python Cookbook Recipe 1.9
# Chris Perkins, Raymond Hettinger
if len(to) == 1: to = to * len(frm)
trans = string.maketrans(frm, to)
if keep is not None:
allchars = string.maketrans('', '')
# delete is expanded to delete everything except
# what is mentioned in set(keep)-set(delete)
delete = allchars.translate(allchars, keep.translate(allchars, delete))
def translate(s):
return s.translate(trans, delete)
return translate

alpha = 'abcdefghijklmnopqrstuvwxyz'
keep_alpha=translator(keep=alpha)

while True:
speech = raw_input("Enter file name:")
wholeFile = open(speech, 'r+').read()
lowlet = wholeFile.lower()
letters = keep_alpha(lowlet)
n = len(letters)
occurrences = collections.defaultdict(int)
for x in letters:
occurrences[x]+=1
for x in occurrences:
print x, occurrences[x], occurrences[x]/float(n)

关于python - python中的字母频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5148903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com