anagram finder / dict mapping question

cokofreedom at gmail.com cokofreedom at gmail.com
Fri May 9 03:45:43 EDT 2008


>>> 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



More information about the Python-list mailing list