Elegant way to compare sequences

前端 未结 8 2014
死守一世寂寞
死守一世寂寞 2021-02-05 20:10

Does python provide an elegant way to check for \"equality\" of sequences of different types? The following work, but they seem rather ugly and verbose for python code:

8条回答
  •  甜味超标
    2021-02-05 20:36

    Since you put the word "equality" in quotes, I assume that you would like to know how the lists are the same and how the are different. Check out difflib which has a SequenceMatcher class:

        sm = difflib.SequenceMatcher(None, a, b)
        for opcode in sm.get_opcodes():
            print "    (%s %d:%d %d:%d)" % opcode
    

    You will get back a sequences of descriptions of the differences. It's fairly simple to turn that into diff-like output.

提交回复
热议问题