在:
from difflib import SequenceMatcher
print('---------------------ksv in long string')
temp='gksvlkdfvjmflkvmoiflksjvmoiflkvmoilfjvmoierlkvjfdsljfiefjvo\
isfvoiafvjfojwfdkvasldkcosxzfjirkjmcoipfvjopsnosjvjrgegrjsdijfowijfoiwjfoiwjfoiwjfoijlksvlkdfvjmfl\
kvmoiflksjvmoiflkvmoilfjvmoierlkvjfdsljfiefjvofegegewtfvasvervvwfjoiw'
print(SequenceMatcher(None, 'ksv',temp).get_matching_blocks())
print('-----------------------long string start with ksv')
temp='ksvlkdfvjmflkvmoiflksjvmoiflkvmoilfjvmoierlkvjfdsljfiefjvo\
isfvoiafvjfojwfdkvasldkcosxzfjirkjmcoipfvjopsnosjvjrgegrjsdijfowijfoiwjfoiwjfoiwjfoijlksvlkdfvjmfl\
kvmoiflksjvmoiflkvmoilfjvmoierlkvjfdsljfiefjvofegegewtfvasvervvwfjoiw'
print(SequenceMatcher(None, 'ksv',temp).get_matching_blocks())
print('-----------------------ksv in short string')
temp='gksvlkdfvjmflkvmoiflksjvmoiflkvmoilfjvmoierlkvjfdsljfiefjvo'
print(SequenceMatcher(None, 'ksv',temp).get_matching_blocks())
出去:
---------------------ksv in long string
[Match(a=3, b=226, size=0)]
-----------------------start with ksv
[Match(a=0, b=0, size=3), Match(a=3, b=225, size=0)]
-----------------------ksv in short string
[Match(a=0, b=1, size=3), Match(a=3, b=59, size=0)]
显然,对于第一个 match_result,'gks' 处于临时状态,但 get_matching_blocks 没有返回该块。
然后我删除了 temp 的第一个“g”,它返回了正确的块。
我尝试缩短温度,但仍然不以“gks”开头,它也返回了正确的块。
所以我很困惑。为什么第一次尝试没有成功?