使用列表理解:
[item for item in x if item not in y]
如果要使用中
-缀语法,则可以执行以下操作:
class MyList(list): def __init__(self, *args): super(MyList, self).__init__(args) def __sub__(self, other): return self.__class__(*[item for item in self if item not in other])
然后可以像这样使用它:
x = MyList(1, 2, 3, 4)y = MyList(2, 5, 2)z = x - y
但是,如果您并非绝对需要列表属性(例如,订购),则只需将集用作其他答案即可。



