cataloging words in text file

Grant Edwards grante at visi.com
Fri Mar 2 18:03:38 EST 2001


In article <v9Vn6.396$z6.33544 at ruti.visi.com>, Grant Edwards 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.

>#!/usr/local/bin/python2.1
>import sys
>d={}
>for w in sys.stdin.read().split():
>    if d.has_key(w):
>        d[w] += 1
>    else:
>        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.

A call to translate() with an appropriate translation table
before the split() solves those issues. ;)

-- 
Grant Edwards                   grante             Yow!  I am NOT a nut....
                                  at               
                               visi.com            



More information about the Python-list mailing list