Using Python, find anagrams for a list of words

前端 未结 22 903
失恋的感觉
失恋的感觉 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:17

    def findanagranfromlistofwords(li):
        dict = {}
        index=0
        for i in range(0,len(li)):
            originalfirst = li[index]
            sortedfirst = ''.join(sorted(str(li[index])))
            for j in range(index+1,len(li)):
                next = ''.join(sorted(str(li[j])))
                print next
                if sortedfirst == next:
                    dict.update({originalfirst:li[j]})
                    print "dict = ",dict
            index+=1
    
        print dict
    
    findanagranfromlistofwords(["car", "tree", "boy", "girl", "arc"])
    

提交回复
热议问题