【发布时间】:2021-08-02 18:27:21
【问题描述】:
我有一个字符串列表和一些文本字符串如下:
my_list = ['en','di','fi','ope']
test_strings = ['you cannot enter', 'the wound is open', 'the house is clean']
如果 test_strings 中的最后一个单词出现在列表中,我想用上面列表中的相应字符串替换它们。我编写了一个 for 循环来捕获模式,但不知道如何进行替换(?)。
for entry in [entry for entry in test_strings]:
if entry.split(' ', 1)[-1].startswith(tuple(my_list)):
output = entry.replace(entry,?)
作为我想要的输出:
output = ['you cannot en', 'the wound is ope', 'the house is clean']
【问题讨论】:
-
请编辑您的问题并显示此输入的示例输出
-
感谢您指出这一点。我添加了一个示例输出
标签: python string list replace