从Python的列表中找出字母频率

时间:2016-10-11 11:54:35

标签: list python-3.x frequency

我试图编写一个程序来计算列表中不同字符的出现次数。我想找到最常见的7个,并且还要计算该字母总数中出现的百分比。

fileOpen = open("lol.txt", 'r')
savedWordData = fileOpen.read()
fileOpen.close()

#To split into chars and function to clear the string from faulty chars
savedWordData = cleanString(savedWordData)
savedWordData = savedWordData.replace(" ", "")
#print(savedWordData)


#Use this to count the total number of chars and find the 7 most common once
from collections import Counter
data = Counter(savedWordData)
print("The 7 most common letters: "  + str(data.most_common(7)))
sumOfAll = sum(data.values())

但不确定我应该如何从这里继续。如何访问数据字典中的值,以便查看每个字母的出现位置?

1 个答案:

答案 0 :(得分:2)

您可以使用most_common,然后在列表中循环以获取值:

In [35]: s = 'ieufisjhfkdfhgdfkjvwoeiweuieanvszudadyuieafhuskdjfhdviurnawuevnskzjdvnziurvzdkjHFiuewhksjnvviuzsdiufwekfvnxkjvnsdv'

In [36]: l = list(s)

In [37]: from collections import Counter

In [38]: data = Counter(l)

In [39]: data.most_common()
Out[39]: 
[('u', 11),
 ('v', 11),
 ('d', 10),
 ('i', 10),
 ('k', 8),
 ('e', 8),
 ('f', 8),
 ('s', 7),
 ('j', 7),
 ('n', 7),
 ('z', 5),
 ('w', 5),
 ('h', 5),
 ('a', 4),
 ('r', 2),
 ('g', 1),
 ('H', 1),
 ('y', 1),
 ('x', 1),
 ('o', 1),
 ('F', 1)]

In [40]: for i in range(0, 7):
    ...:     print(data.most_common()[i])
    ...:     
('u', 11)
('v', 11)
('d', 10)
('i', 10)
('k', 8)
('e', 8)
('f', 8)

第一个值是字母,第二个值是出现次数。