如何替换字符串列表中字符串的具体子字符串?

柔韧的

我有以下字符串列表:

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章