您必须遍历字符串列表,而不是如下所示的字符串。
forbidden_words = ["And","is","to","from","the"]
sentence = "And thus brought us to this conclusion from the existing inference".split()
filt_text = [ w for w in sentence if w not in forbidden_words]
filt_sentence = ' '.join(filt_text)
print(filt_sentence )