在python中计算abody文本中字母频率图的代码

时间:2016-12-21 06:49:26

标签: python-2.7

我正在编写一个程序,用于生成文本正文中字母的频率图。但是,我的代码中有一个错误,我无法发现它。任何想法?

def letter_count(word,freqs,pmarks):
for char in word:
    freqs[char]+=1
def letter_freq(fname):
fhand = open(fname)
freqs = dict()
alpha = list(string.uppercase[:26])
for let in alpha: freqs[let] = freqs.get(let,0)
for line in fhand:
    line = line.rstrip()
    words = line.split()
    pmarks = list(string.punctuation)
    words = [word.upper() for word in words]
    for word in words:
        letter_count(word,freqs,pmarks)                                                                                   
 fhand.close()

return freqs.values

1 个答案:

答案 0 :(得分:0)

您正在致电

freqs[char]+=1

with char ='。'没有初始化值freqs ['。'] = 0

您应该在第3行之前检查密钥是否已经存在,因为您只能对字典的现有密钥执行+ = 1操作。

类似于:

for char in word:
    if freqs.has_key(char):
        freqs[char]+=1

Python: how can I check if the key of an dictionary exists?