Count frequency of letters in a text file
Use collections.Counter()
:
from collections import Counter
with open(file) as f:
c = Counter()
for line in f:
c += Counter(line)
If the file is not so large, you can read all of it into memory as a string and convert it into a Counter
object in one line of code:
c = Counter(f.read())
Example:
>>> c = Counter()
>>> c += Counter('aaabbbcccddd eee fff ggg')
>>> c
Counter({'a': 3, ' ': 3, 'c': 3, 'b': 3, 'e': 3, 'd': 3, 'g': 3, 'f': 3})
>>> c += Counter('aaabbbccc')
Counter({'a': 6, 'c': 6, 'b': 6, ' ': 3, 'e': 3, 'd': 3, 'g': 3, 'f': 3})
or use the count()
method of strings:
from string import ascii_lowercase # ascii_lowercase =='abcdefghijklmnopqrstuvwxyz'
with open(file) as f:
text = f.read().strip()
dic = {}
for x in ascii_lowercase:
dic[x] = text.count(x)
Use a dictionary - basically letters[char]++