Pandas-list-mapping

Reason is the light and the light of life.

Jerry Su Nov 01, 2021 1 mins
text = '合同有效期:  2021年9月至2022年9月'
text
'合同有效期:  2021年9月至2022年9月'
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)
len(text)
23
start = origin.find(text)
end = start + len(text)
start, end
(3, 26)
origin[3:26]
'合同有效期:  2021年9月至2022年9月'
ll[mapping[start]:mapping[end - 1] + 1]
['合同有效期:', '  ', '2', '02', '1', '年', '9', '月至2', '02', '2', '年', '9', '月']

Read more:

Related posts: