我正在尝试使用 python 代码获得如下所示的输出...有什么建议吗?

I am trying to get output like below using pythone code...any suggestions?

我正在尝试使用 pythone 代码获得如下所示的输出...有什么建议吗?

list=["ABCPMCABC CMD","CMD ABC"] list2=["ABC","CMD"]

输出: [ABCABC CMD,CMD ABC]

您可以使用 re 模块:

import re

list1 = ["ABCPMCABCCMD", "CMDABC", "ABCMD"]
list2 = ["ABC", "CMD"]

r = re.compile("|".join(re.escape(w) for w in list2))
out = ["".join(r.findall(word)) for word in list1]
print(out)

打印:

['ABCABCCMD', 'CMDABC', 'ABC']