text = '合同有效期: 2021年9月至2022年9月'
text
runs = ['3.', ' ', '合同有效期:', ' ', '2', '02', '1', '年', '9', '月至2', '02', '2', '年', '9', '月']
origin = ''.join(ll)
origin
'3. 合同有效期: 2021年9月至2022年9月'
def runs_mapping(runs):
mapping, start = {}, 0
for idx, run in enumerate(runs):
for i in range(start, start + len(run)):
mapping[i] = idx
start += len(run)
return mapping
mapping = runs_mapping(runs)
start = origin.find(text)
end = start + len(text)
ll[mapping[start]:mapping[end - 1] + 1]
['合同有效期:', ' ', '2', '02', '1', '年', '9', '月至2', '02', '2', '年', '9', '月']
Share on:
Twitter
❄ Facebook
❄ Email