guete
guete

Reputation: 79

python difflib print only matches part between two strings

I am totally new with python, and I need some help about difflib. I tried read the documentation, but for me is not easy to understand the docs.

I would like compare two strings, and I want the output is only the matches prefix-part between two string (do not print the differences) .

Example:

t1 = "hello my name is Tom" 
t2 = "hello his name is Sawyer"

expected output is: "hello "

I tried by following approaches, but the output is not what I want because it print the output as array and also print not only matches part:

#!/usr/bin/python

import difflib
t1 = "hello my name is Tom"
t2 = "hello his name is Sawyer"
seq = difflib.Differ()
seq = seq.compare(t1,t2)
print list(seq)

other example-2:

t1 = "20180628-153020"
t2 = "20180628-173020"

expected output printout: "20180628-1" and the suffix "3020" even both part of string matches the position and characters, it should be ignored.

Please help me.. thank you...

update 2018/07/02

I wrote a small code to get only similar part of prefix between two strings (without using takewhile as 1st answer from @Eugene), but I think the 2nd code written by @Eugene in the answer is more shorter and efficient than mine. here is my code:

def getprefix(s1, s2):
    pref = ""
    ls1 = list(s1)
    ls2 = list(s2)
    i=0
    while i < len(ls1):
        if ls1[i] not in ls2[i]:
            return pref
        pref += ls1[i]
        i += 1

Upvotes: 2

Views: 895

Answers (1)

Eugene Primako
Eugene Primako

Reputation: 2817

In [1]: from itertools import takewhile
In [2]: ''.join(a for (a, b) in takewhile(lambda (a, b): a == b, zip(t1, t2)))
Out[2]: '20180628-1'

Here we zip two strings together and iterate on them while their prefixes are equal.

Upd. Equivalent solution not using itertools:

result = []
for a, b in zip(t1, t2):
    if a != b:
        break
    result.append(a)
return ''.join(result)

Upvotes: 1

Related Questions