try:
import re
pat2 = re.compile('(Ad+)|(^d.*.$)')
for l in range(1,len(lst)):
a1, val1 = re.findall(pat2, lst[l-1])[0]
a2, val2 = re.findall(pat2, lst[l])[0]
if not a2:
lst[l] = f"{a1}.{lst[l]}"
lst:
['A1.',
'A1.0.',
'A1.1.',
'A1.2.',
'A2.',
'A2.1.1.',
'A3.',
'A3.0.',
'A3.1.1.1.']
explanation:
- I am changing the original list.
- I will take the previous list element find the prefix and if my current doesn't have one I will append the previous one prefix.
pat2 will capture
'1.0.0.'
==> [('', '1.0.0.')]
'A1.0.0.'
==> [('A1', '')]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…