python3 实现统计单词表中每个字母的出现频率

作为python字典与数组概念的运用,统计字母表中每个字母出现的频率,作为练习再合适不过。
解决问题过程中需要用到的知识点包括:字典的创建、增添元素,数组的创建、增添元素,数组的遍历等
这个问题解决的思路为:首先从文件中按行依次读入单词,去除换行符后添加到数组 new_list 中。依次遍历数组 new_list 的每一个字符串,将每个字符串连同上一次循环中的频率统计结果 old_d (old_d在遍历new_list之前进行初始化)一起作为实参传递给频率统计函数 histogram()。histogram()函数在上一轮频率统计基础上得出本轮频率统计结果,结果通过字典 d 传回,将值赋给 old_d 。直到遍历完new_list,再将 old_d 统计结果打印。

'''transform string into dictionary

s is input string 
d is dictionary to restore every bit in string
'''
def histogram(s, old_d):
    d = old_d
    for c in s:
        d[c] = d.get(c, 0) + 1 
    return d

'''This function can calculate the frequency of every letter in alphabet

'''            
fin = open("words.txt")
new_list = []
for line in fin:
    rs = line.rstrip('\n')  #delete the '\n' after every letter
    new_list.append(rs)  # new_list is used to restore letters
old_d = dict() # initialize the dictionary 
for i in range(len(new_list)): #calculate the letter 
#frequency of every word
    old_d = histogram(new_list[i], old_d) #old_d is used to 
    #restore letter frequency before new_list[i]
print(old_d) 

你可能感兴趣的:(python算法)