Problem loading a file of words

Terrance N. Phillip mediocre_person at hotmail.com
Mon Jul 25 00:19:48 EDT 2005


Kevin,
	I'm pretty new to Python too. I'm not sure why you're seeing this 
problem... is it possible that this is an "out-by-one" error? Is 
zymotechnics the *last* word in dictionary.txt? Try this slightly 
simplified version of your program and see if you have the same problem....

def sort_string(word):
     '''Returns word in lowercase sorted alphabetically'''
     return "".join(sorted(list(word.lower())))

dictionary = {}
f = open('/usr/bin/words') # or whatever file you like
for line in f:
         sline = sort_string(line[:-1])
         if sline in dictionary:
                 dictionary[sline].append(line)
         else:
                 dictionary[sline] = [line]
f.close()

lookup = raw_input('Enter a scrambled word : ')
while lookup:
         try:
             results = dictionary[sort_string(lookup)]
             for x in results:
                 print x,
             print
         except:
             print "?????"
         lookup = raw_input('Enter a scrambled word : ')


Good luck,

Nick.



More information about the Python-list mailing list