Trying to come up with python anagram function

后端 未结 7 2086
梦谈多话
梦谈多话 2021-01-23 14:02

What I\'m trying to do is if I have a list like:

[\"lime\", \"mile\", \"liem\", \"tag\", \"gat\", \"goat\", \"math\"]

I want to write a functio

7条回答
  •  轮回少年
    2021-01-23 14:27

    an approach identifying the words by the frozenset of the characters:

    from collections import defaultdict
    
    wordlist = ["lime", "mile", "liem", "tag", "gat", "goat", "math"]
    
    worddict = defaultdict(list) 
    for word in wordlist:
        worddict[frozenset(word)].append(word)
    
    anagrams = [words for words in worddict.values() if len(words) > 1]
    print(anagrams)
    
    # [['lime', 'mile', 'liem'], ['tag', 'gat']]
    

    the output is not yet quite what you wanted, but flattening that list is easy if you prefer that.


    update after comments:

    the solution above will not handle words with repeating characters well. but this will (this time the key of the dictionary is just the string consisting of the sorted letters):

    for word in wordlist:
        worddict[''.join(sorted(word))].append(word)
    

提交回复
热议问题