Python內建庫實現文字比較並返回差異位置座標

walteronly1發表於2024-06-24

參考: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

相關文章