anagram finder / dict mapping question

Arnaud Delobelle arnodel at googlemail.com
Fri May 9 15:48:05 EDT 2008


umpsumps at gmail.com writes:

> On May 9, 1:45 am, cokofree... at gmail.com wrote:
>> >>> key = ''.join(sorted(word))
>>
>> I tend to strip and lower the word as well, otherwise "Hello" and
>> "hello" do not compare...depends on what you want though!
>> Plus you might get a lot of "word\n" as keys...
>>
>> My technique is the this way
>>
>> def anagram_finder(words):
>>     anagrams = {}
>>     for word in words:
>>         word = word.strip()
>>         key = ''.join(sorted(word.lower()))
>>         anagrams.setdefault(key, []).append(word)
>>     return anagrams
>
> What would be the best method to print the top results, the one's that
> had the highest amount of anagrams??  Create a new histogram dict?

You can use the max() function to find the biggest list of anagrams:

top_results = max(anagrams.itervalues(), key=len)

-- 
Arnaud



More information about the Python-list mailing list