我有以下字符串列表:
list_of_str = ['Notification message', 'Warning message', 'This is the |xxx - show| message.', 'Notification message is defined by |xxx - show|', 'Notification message']
如何获取最接近尾部并包含的字符串show|
,并替换show|
为Placeholder|
?
预期结果:
list_of_str = ['Notification message', 'Warning message', 'This is the |xxx - show| message.', 'Notification message is defined by |xxx - Placeholder|', 'Notification message']
反向迭代,查找替换:
for i, s in enumerate(reversed(list_of_str), 1):
if 'show|' in s:
list_of_str[-i] = s.replace('show|', 'Placeholder|')
break
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句