您需要使用理解 if 子句,例如:代码:aa = [x for x in a if x not in delete]测试代码:a = ["I", "have", "something", "to", "buy"]delete = ["I", "have"]aa = [x for x in a if x not in delete]print(aa)结果:['something', 'to', 'buy']
尝试打印(a)而不是打印(aa)当您执行 a.remove(x) 时,它会将其从数组 'a' 中删除。 a = ["I", "have", "something", "to", "buy"] delete = ["I", "have"] aa = [a.remove(x) for x in delete] print(a)