Problem loading a file of words

teoryn teoryn at gmail.com
Mon Jul 25 09:27:33 EDT 2005


Thanks to everyone for all the help!

Here's the (at least for now) final script, although note I'm using
2.3.5, not 2.4, so I can't use some of the tips that were given.

#!/usr/bin/python
# Filename: unscram.py

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

print 'Building dictionary...',

dictionary = { }

f = file('/usr/share/dict/words', 'r')

for line in f.readlines():
     line = line.strip()  # remove whitespace at both ends
     if line:  # line is not the empty string
          line = line.lower()
          sline = sort_string(line)
          if sline in dictionary:
               dictionary[sline].append(line)
               #print 'Added %s to key %s' % (line,sline)
          else:
               dictionary[sline] = [line]
               #print 'Created key %s for %s' % (sline,line)
f.close()

print 'Ready!'

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 : ')



As for the end of the file idea, that word wasn't at the end of the
file, and there was a blank line, so that's out of the question. The
word list I was using was 272,520 words long, and I got it a while back
when doing this same thing in java, but as you can see now I'm just
using /usr/share/dict/words which I found after not finding it in the
place listed in Nick's comment.

I'm still lost as to why my old code would only work for the small
file, and another interesting note is that with the larger file, it
would only write "zzz for zzz" (or whatever each word was) instead of
"Created key zzz for zzz". However, it works now, so I'm happy.

Thanks for all the help,
Kevin




More information about the Python-list mailing list