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
答案 0 :(得分:0)
您正在致电
freqs[char]+=1
with char ='。'没有初始化值freqs ['。'] = 0
您应该在第3行之前检查密钥是否已经存在,因为您只能对字典的现有密钥执行+ = 1操作。
类似于:
for char in word:
if freqs.has_key(char):
freqs[char]+=1