Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count frequency of letters in a text file [closed]

Tags:

python

text

In python, how can I iterate through a text file and count the number of occurrences of each letter? I realise I could just use a 'for x in file' statement to go through it and then set up 26 or so if elif statements, but surely there is a better way to do it?

Thanks.

like image 897
Muzz5 Avatar asked Sep 09 '12 19:09

Muzz5


2 Answers

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)
like image 200
Ashwini Chaudhary Avatar answered Oct 02 '22 05:10

Ashwini Chaudhary


Use a dictionary - basically letters[char]++

like image 36
djechlin Avatar answered Oct 02 '22 06:10

djechlin