Using Python, find anagrams for a list of words

前端 未结 22 906
失恋的感觉
失恋的感觉 2020-12-13 01:11

If I have a list of strings for example:

[\"car\", \"tree\", \"boy\", \"girl\", \"arc\"...]

What should I do in order to find anagrams in t

22条回答
  •  甜味超标
    2020-12-13 01:36

    In order to do this for 2 strings you can do this:

    def isAnagram(str1, str2):
        str1_list = list(str1)
        str1_list.sort()
        str2_list = list(str2)
        str2_list.sort()
    
        return (str1_list == str2_list)
    

    As for the iteration on the list, it is pretty straight forward

提交回复
热议问题