如何用其他字符串列表替换字符串?

有没有办法做到这一点?

list = ['test inc', 'abc', '123 corp']
words_to_filter = ['inc', 'corp']
list.replace(words_to_filter,'')

我的预期结果应该是:

test, abc, 123

现在我得到:

TypeError: replace() argument 1 must be str, not list

回答

怎么样 :

for i in range(len(list)):
    for word in words_to_filter:
         list[i] = list[i].replace(word, "").strip()

作为一个班轮:

list = [" ".join([x for x in item.split() if x not in words_to_filter]) for item in list]


以上是如何用其他字符串列表替换字符串?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>