Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试使用 pythone 代码获得如下输出...有什么建议吗?
list=["ABCPMCABCCMD","CMDABC"] list2=["ABC","CMD"]
输出:[ABCABCCMD,CMDABC]
您可以使用re模块:
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']