Python内置库实现文本比较并返回差异位置坐标

参考:https://docs.python.org/zh-cn/3/library/difflib.html#sequencematcher-objects

from difflib import *

a='abcdefg'
b='ab12f6g8a'

s = SequenceMatcher(None, a, b)
res=s.get_matching_blocks()
print(res)
#输出[Match(a=0, b=0, size=2), Match(a=5, b=4, size=1), Match(a=6, b=6, size=1), Match(a=7, b=9, size=0)]

for i in res:
    print(i.a)
#输出
0
5
6
7

 

posted @ 2024-06-24 14:57  walteronly1  阅读(2)  评论(0编辑  收藏  举报