cataloging words in text file

Grant Edwards grante at
Fri Mar 2 23:29:47 CET 2001

In article <3AA0124E.74A39783 at>, Stephen Boulet wrote:

>I remember this homework assignment for my data structures (c++)
>class: read in a large file, and create a data structure containing
>every word in the file and the number of times it appears.
>I was wondering how to do this in python. In c++ we had to do it
>with hash tables and b-trees.
>Can you do it with dictionaries in python, with the key as the word
>and the data the number of times it appears?


import sys
for w in
    if d.has_key(w):
        d[w] += 1
        d[w] = 1
print d    

You may want something a little more sophisticated than
split(), since this solution is case sensitive and treats
"foo" and "foo." as two different words.

Grant Edwards                   grante             Yow!  .. I don't understand
                                  at               the HUMOR of the THREE

More information about the Python-list mailing list