anagram finder / dict mapping question

Kam-Hung Soh kamhung.soh at gmail.com
Wed May 7 21:25:53 EDT 2008


On Thu, 08 May 2008 11:02:12 +1000, dave <squareswallower at 1ya2hoo3.net>  
wrote:

> Hi All,
>
> I wrote a program that takes a string sequence and finds all the words  
> 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 inside 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 Salariman</a>



More information about the Python-list mailing list