Count letter differences of two strings

后端 未结 11 706
既然无缘
既然无缘 2020-12-09 17:11

This is the behaviour I want:

a: IGADKYFHARGNYDAA
c: KGADKYFHARGNYEAA
2 difference(s).
11条回答
  •  醉酒成梦
    2020-12-09 17:18

    Python has the excellent difflib, which should provide the needed functionnality.

    Here's sample usage from the documentation:

    import difflib  # Works for python >= 2.1
    
    >>> s = difflib.SequenceMatcher(lambda x: x == " ",
    ...                     "private Thread currentThread;",
    ...                     "private volatile Thread currentThread;")
    >>> for block in s.get_matching_blocks():
    ...     print "a[%d] and b[%d] match for %d elements" % block
    a[0] and b[0] match for 8 elements
    a[8] and b[17] match for 21 elements
    a[29] and b[38] match for 0 elements    
    

提交回复
热议问题