python difflib print only matches part between two strings


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:


takewhile


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




1 Answer
1


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:


itertools


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





wow!! superr!!! exactly what I need.Thank you, Sir! :)
– guete
Jun 28 at 15:40






@guete Then you might want to mark this answer as accepted ;)
– Eugene Primako
Jun 28 at 15:47





absolutely Sir! Thanks :)
– guete
Jun 28 at 15:49





btw @Eugene, what is other approaches if my live system don't have itertools? I also don't have access to add new modules to the system. any idea?.
– guete
Jun 29 at 9:06






@guete itertools is not a custom module but a part of standard python library. So I guess you can access it. Any way, you can replace takewhile with for (I'll update my answer)
– Eugene Primako
Jun 29 at 9:55


takewhile


for






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV