Hey, I am working on a program for my class where we are supposed to count the letters that occur in a given string, with decreasing frequency. For instance, it would say there are 150 A's, 234 B's, 25 C's, etc. Plus, it would then become possible to sort the results so that the most frequent letter would be first and then decrease in frequency. For example, 392 E's, 355 O's, 290 R's, 234 B's, ... 2 J's, etc.

This is the exact assignment from my professor:

I want you to take a selection of text, and build a string that has the letters of the alphabet in order of decreasing frequency. This would be useful in a letter-frequency attack on the Cryptoquip in the Post-Dispatch.

This is really a lot easier than it looks. My solution is 15 lines, exclusive of the text sample. There is a lot of power available in the string and list methods.

Here’s the idea: Take a string, presumably a very long one. Make it into all upper case. Then, in a loop that goes through the letters of the alphabet, find the count for each letter. Package that number with the letter in a short list. Very important: the number is first, letter second. Put each of the short lists into one long list. It might look like this:

[ [212, ‘A’], [85, ‘B’], [77, ‘C’], … , [5, ‘Z’] ]

If you now sort that list, you will get it in order by increasing number. Reverse it to get decreasing order, with the most frequent letter first. Now, go through and extract just the letters and make it into a string, and print out that string.

Your string is likely to start with ‘ETAOINSHRDLU’, or close to that, as the twelve most frequent characters. Don’t be too upset if you don’t; you probably won’t have a large enough text sample.

Test with a respectable size sample of text. You might try a paper you have written. You can probably make it into one long paragraph and paste it in; be careful, as embedded quotes will mess things up. You can simply delete all embedded quotes, as they will not affect the results anyway. At a later date, we may try your program on a novel, but we need files first.

Now, I have managed to create a program where I can count the letters, but I can't sort my results or make anything into a list. At least, using my current method, if I try to make anything into a list, I get an error message of :

Traceback (most recent call last):
File "I:/program 5 count.py", line 18, in <module>
sorted(lettercount(String2))
TypeError: 'NoneType' object is not iterable

My code is as follows:

String = " Many critics consider........"

import string

String2 = String.upper()

alphabet = string.ascii_uppercase



def lettercount(str):
    for letter in alphabet:
        print ([str.count(letter) , letter])
        

sorted(lettercount(String2))

Any help? D: It's due tomorrow morning.....aka a few hours.

You do not have a return statement in your lettercount function, so it returns None.
Otherwise:
result="".join(i[1] for i in sorted(lettercount(String2)))

import string

string_in = " Many critics consider........"

alphabet = set(string.ascii_uppercase)
string_in = string_in.upper()

print sorted(((string_in.count(c),c)
             for c in set(string_in)
             if c in alphabet), reverse = True )
This article has been dead for over six months. Start a new discussion instead.