cataloging words in text file
Grant Edwards
grante at visi.com
Fri Mar 2 17:29:47 EST 2001
In article <3AA0124E.74A39783 at comm.mot.com>, 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?
Sure:
#!/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.
--
Grant Edwards grante Yow! .. I don't understand
at the HUMOR of the THREE
visi.com STOOGES!!
More information about the Python-list
mailing list