Help with inverted dictionary

Dark Cowherd darkcowherd at gmail.com
Tue Jul 12 13:10:18 EDT 2005


As Steven said this looks too much like home work
But what the heck I am also learning python. So I wrote a small
program. A very small program. I am fairly new to Python, I am stunned
each time to see how small programs like this can be.

Since I am also learning can somebody comment if anything here is not
Pythonesque.

dictwords = dict()
for line in open('testfile.txt','r'):
   for word in line.rstrip('\n').split():
       dictwords.setdefault(word,set()).update((line.rstrip('\n'),))
for wordfound in dictwords.items():
   open(wordfound[0],'w').write('\n'.join(wordfound[1]))



More information about the Python-list mailing list