On 2008-05-07 19:25:53 -0600, "Kam-Hung Soh" <[EMAIL PROTECTED]>

said:

On Thu, 08 May 2008 11:02:12 +1000, dave <[EMAIL PROTECTED]
t>
wrote:

Hi All,
 I wrote a program that takes a string sequence and finds all the wo
rds

inside a text file (one word per line) and prints them:
 def anagfind(letters):         #find anagrams of these letters
        fin = open('text.txt')  #one word per line file
        wordbox = []             #this is where the words will go
        for line in fin:
                word = line.strip()
                count = 0
                for char in letters:
                        if char not in word:
                                break
                        else:
                                count += 1
                        if count == len(word):
                                wordbox.append(word)
        return wordbox
 Now I'd like to modify the code to naturally find all anagrams insi
de
a
wordlist.  What would be the best way to do this?  Using Hints?  Is
it

possible to iterate over dict keys?  How can I make a dict that maps


from a set of letters to a list of words that are spelled from those


letters?  Wouldn't I need to make the set of letters a key in a dict
?
 As always - Thanks for helping someone trying to learn...
 Dave

 Suggestion: for each word, sort their characters and use them as the

dictionary key.  If two words have the same combination of characters
,
then they are anagrams.  For example: "edam" and "made" are anagrams
because they have the letters 'a', 'd', 'e' and 'm'.
 Refer "Programming Pearls" by Jon Bentley.
 --
Kam-Hung Soh <a href="http://kamhungsoh.com/blog";>Software Salarima
n</
a>



Your code is always going to return the same list because every word is  an
anagram of itself.

Tip: Create a list for each dictionary key, then add a word to the list  if
that word is not in the list.  So:

mapdic('adem') --> ["edam", "made"]

P.S. When replying, the convention is to add your response to the bottom ,
not top of the message.

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog";>Software Salariman</ a>

I got it!  Thanks for all your help!!!  Please tell me what you think:

def anafind():
           fin = open('short.txt')                      #one word per line
           mapdic = {}                                  #this dic maps letters 
to anagrams
           for line in fin:
                       line = line.strip()
                       templist = sorted(list(line))    #sort letters
                       newtlist = (''.join(templist))   #join letters
                       if newtlist not in mapdic:
                                   mapdic[newtlist] = [line]
                       if line not in mapdic[newtlist]:
                                   mapdic[newtlist].append(line)
           for value in mapdic.values():
                       if len(value) <= 1:
                                   pass
                       else:
                                   print value


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to