Find out the letter frequency in from a list in Python
Question: I'm trying to code a program that will count the occurence of different chars
in a list. I want to find the 7 most common once and also want to count the %
of the occurence of that letter of the total amount of letters.

    
    
    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())
    

But not sure how I should continue from here. How do I access the values from
the data dict so I can see the occurrence of each letter?


Answer: You can use
[`most_common`](https://docs.python.org/2/library/collections.html#collections.Counter.most_common),
and then loop on the list to get the values :

    
    
    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)
    

The first value is the letter, the second one is the number of occurrence.

