0

The function prints out the individual frequency for letters in a file but I cant get it to ignore non letters, I only want it to count letters when working out the percentage frequency of each letter. This what I have so far:

from string import ascii_lowercase as lowercase

def calcFrequencies(file):
    """Enter file name in quotations. Shows the frequency of letters in a file"""
    infile = open(file)
    text = infile.read()
    text = text.lower()

    text_length = len(text)
    counts = [0]*26

    for i in range(26):
        char=lowercase[i]
        counts[i] = 100*text.count(char)/text_length
        print("{:.1f}% of the characters are '{}'".format(counts[i],char))
    infile.close()
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Foflo
  • 39
  • 1
  • 9

2 Answers2

3

Use filter

>>> text = "abcd1234efg"
>>> filter(str.isalpha, text)
'abcdefg'
mhlester
  • 22,781
  • 10
  • 52
  • 75
1

You could use the join method with a list comprehension (faster than a genexp) to reassign the string with only the alphabetic characters before counting:

text = ''.join([char for char in text if char.isalpha()])
jayelm
  • 7,236
  • 5
  • 43
  • 61
  • @Foflo just be advised, if you care about speed I believe mhlester's solution is faster. – jayelm Jan 30 '14 at 02:35
  • I think he was downvoted before my answer. That said it works so the downvote is unwarranted – mhlester Jan 30 '14 at 02:38
  • I was not the down voter. But you should remember, passing a generator is slower than passing a List to `str.join`. So @aj8uppal 's answer would end up to be faster than yours. – Abhijit Jan 30 '14 at 02:41
  • @Abhijit I actually didn't know that, could you tell me why? – jayelm Jan 30 '14 at 02:42
  • 2
    @jmu303: Check this [SO answer](http://stackoverflow.com/a/9061024/977038) and the Question [List comprehension vs generator expression's weird timeit results?](http://stackoverflow.com/q/11964130/977038) – Abhijit Jan 30 '14 at 02:45