我需要创建一个函数,该函数将文本文件作为输入并返回大小为26的矢量,频率以每个字符(a到z)的百分比表示。这必须对大小写不敏感。所有其他字母(例如å)和符号应该被忽略。确定相对字母频率
我试过使用这里的一些答案,特别是'雅各'的答案。 Determining Letter Frequency Of Cipher Text
这是我到目前为止的代码:
def letterFrequency(filename):
#f: the text file is converted to lowercase
f=filename.lower()
#n: the sum of the letters in the text file
n=float(len(f))
import collections
dic=collections.defaultdict(int)
#the absolute frequencies
for x in f:
dic[x]+=1
#the relative frequencies
from string import ascii_lowercase
for x in ascii_lowercase:
return x,(dic[x]/n)*100
例如,如果我试试这个:
print(letterFrequency('I have no idea'))
>>> ('a',14.285714)
为什么它不能打印的字母都相对值?还有不在字符串中的字母,比如我的例子中的z?
以及如何让我的代码打印大小为26的矢量?
编辑:我试过使用计数器,但它打印('a':14.2857)和字母混合顺序。我只需要按顺序排列字母的相对频率!
谢谢你,这个工作..但我如何删除在打印结果中的逗号?它打印[数字,数字,数字],但我真的想得到[数字号码]像数组 – Gliz
@Gliz使用'letters.append((dic [x]/n)* 100)'并打印它使用'for e in letterFrequency('我不知道'):print(e,end ='')'。那是你要的吗? –