该函数打印出文件中字母的单独频率但我无法忽略非字母,我只想在计算每个字母的百分比频率时计算字母数。这就是我到目前为止:
from string import ascii_lowercase as lowercase
def calcFrequencies(file):
"""Enter file name in quotations. Shows the frequency of letters in a file"""
infile = open(file)
text = infile.read()
text = text.lower()
text_length = len(text)
counts = [0]*26
for i in range(26):
char=lowercase[i]
counts[i] = 100*text.count(char)/text_length
print("{:.1f}% of the characters are '{}'".format(counts[i],char))
infile.close()
答案 0 :(得分:3)
使用filter
>>> text = "abcd1234efg"
>>> filter(str.isalpha, text)
'abcdefg'
答案 1 :(得分:1)
您可以使用join
方法使用列表推导(比genexp更快)在计数之前仅使用字母字符重新分配字符串:
text = ''.join([char for char in text if char.isalpha()])