2 回答
TA贡献1893条经验 获得超10个赞
基于@ r.user.05apr的一个很好的主意,即一个字符一个字符地遍历整个输入字符串,我想添加一个可能性以将其概括为任意长列表start_end:
s = 'ABCDEFGHIJKL'
res = list()
for nr, sub in enumerate(s):
res.append(sub)
try:
i = [nr in x for x in start_end].index(True)
res.append('-(c' + str(i) + ')-')
except:
pass
res = ''.join(res)
print(res)
# AB-(c0)-C-(c1)-D-(c0)-E-(c1)-F-(c2)-G-(c2)-HIJK
TA贡献2037条经验 获得超6个赞
希望能帮助到你:
s = 'ABCDEFGHIJKL'
res = list()
for nr, sub in enumerate(s):
res.append(sub)
if nr in (1, 3):
res.append('-(c0)-')
elif nr in (2, 4):
res.append('-(c1)-')
elif nr in (5, 6):
res.append('-(c2)-')
res = ''.join(res)
print(res)
# AB-(c0)-C-(c1)-D-(c0)-E-(c1)-F-(c2)-G-(c2)-HIJKL
添加回答
举报