Find the position of difference between two strings

前端 未结 6 1572
别那么骄傲
别那么骄傲 2021-01-04 00:08

I have two strings of equal length, how can I find all the locations where the strings are different?

For example, \"HELPMEPLZ\" and \"HELPNEPLX\" are different at p

相关标签:
6条回答
  • 2021-01-04 00:10

    If you store the two strings in a and b, you can loop through all the items and check for inequality.

    python interactive interpreter:

    >>> for i in range(len(a)):
    ...   if a[i] != b[i]: print i, a[i], b[i]
    ... 
    4 M N
    8 Z X
    

    Another way to do this is with list comprehensions. It's all in one line, and the output is a list.

    >>> [i for i in range(len(a)) if a[i] != b[i]]
    [4, 8]
    

    That makes it really easy to wrap into a function, which makes calling it on a variety of inputs easy.

    >>> def dif(a, b):
    ...     return [i for i in range(len(a)) if a[i] != b[i]]
    ...
    >>> dif('HELPMEPLZ', 'HELPNEPLX')
    [4, 8]
    >>> dif('stackoverflow', 'stacklavaflow')
    [5, 6, 7, 8]
    
    0 讨论(0)
  • 2021-01-04 00:13

    Python really comes with batteries included. Have a look at difflib

    >>> import difflib
    >>> a='HELPMEPLZ'
    >>> b='HELPNEPLX'
    >>> s = difflib.SequenceMatcher(None, a, b)
    >>> for block in s.get_matching_blocks():
    ...     print block
    Match(a=0, b=0, size=4)
    Match(a=5, b=5, size=3)
    Match(a=9, b=9, size=0)
    

    difflib is very powerful and a some study of the documentation is really recommended.

    0 讨论(0)
  • 2021-01-04 00:27

    Easiest way is to split data into two char arrays and then loop through comparing the letters and return the index when the two chars do not equal each other.

    This method will work fine as long as both strings are equal in length.

    0 讨论(0)
  • 2021-01-04 00:28

    Try this:

    s1 = 'HELPMEPLZ'
    s2 = 'HELPNEPLX'
    [i for i in xrange(len(s1)) if s1[i] != s2[i]]
    

    It will return:

    > [4, 8]
    

    The above solution will return a list with the indexes in sorted order, won't create any unnecessary intermediate data structures and it will work on Python 2.3 - 2.7. For Python 3.x replace xrange for range.

    0 讨论(0)
  • 2021-01-04 00:37
    >>> from itertools import izip
    >>> s1 = 'HELPMEPLZ'
    >>> s2 = 'HELPNEPLX'
    >>> [i for i,(a1,a2)  in enumerate(izip(s1,s2)) if a1!=a2]
    [4, 8]
    
    0 讨论(0)
  • 2021-01-04 00:37

    Pair up the strings character-by-character and iterate over this collection together with a counting index. Test whether the characters in each pair differ; if they do, output the index of where.

    Using Python builtin functions you can do this neatly in one line:

    >>> x = 'HELPMEPLZ'
    >>> y = 'HELPNEPLX'
    >>> {i for i, (left, right) in enumerate(zip(x,y)) if left != right}
    {8, 4}
    
    0 讨论(0)
提交回复
热议问题