如何用其他字符串列表替换字符串?
有没有办法做到这一点?
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]