[Tutor] Letter Frequency Count.

Doug Stanfield DOUGS@oceanic.com
Sat, 8 Jan 2000 08:02:07 -1000


A few comments on your start and then about your approach:

> stringette = 0
> while stringette < len(emperor)+1:
>     individual = emperor[stringette:stringette+1]
      # ... some stuff processing individual...
>     stringette = stringette+1

This construct is sort of like:

for individual in emperor:
      # ... some stuff processing individual...

Where individual should be each single character from the file.  In your
case this won't work straight off because you've read the whole file in. You
might need to do the following to get to each character assuming you just
want to throw away newlines:

for line in emperor:
      for individual in line:
            # ... process individual ...

I hope a lot more understandable.  As to how you should process each
character, I'd suggest a dictionary.  Create an empty dictionary at the top
("countDict = {}").  Once you've tested to see if you want to increment a
count ("if individual in simpsons:"), use the character as the key into the
dictionary:

              if countDict.has_key(individual):
                    countDict[individual] = countDict[individual] + 1
              else:
                    countDict[individual] = 1

In many cases like this Python makes it easy to forget about using indices
and multiple data constructs to accomplish things.

Hope this helped.

-Doug-