私信  •  关注

balu

balu 最近创建的主题
balu 最近回复了
3 年前
回复了 balu 创建的主题 » Python不会删除单词,但适用于单个字母

您必须遍历字符串列表,而不是如下所示的字符串。

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 )