Trying to come up with python anagram function

后端 未结 7 2015
梦谈多话
梦谈多话 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:07

    The easy way to analyses anagram words is to put them in alphabetic order.So you create a second list with alphabetic ordered words.

    ['lime', 'mile', 'liem', 'tag', 'gat']
    
    index = 0
    b = []
    for i in a:
        b.insert(index, ''.join(sorted(i)))
        index = index + 1
    
    ['eilm', 'eilm', 'eilm', 'agt', 'agt']
    

    I think you can have more pythonesque code than the one i give you, but i think the important thing for you is to order letters in the word.

    Now you can do something to analyse your anagrams

提交回复
热议问题