我想取列表x和y的差值:

>>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> y = [1, 3, 5, 7, 9]  
>>> x - y
# should return [0, 2, 4, 6, 8]

当前回答

@aaronasterling提供的答案看起来不错,但是,它与列表的默认接口不兼容:x = MyList(1,2,3,4) vs x = MyList([1,2,3,4])。因此,下面的代码可以用作更友好的python列表:

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

其他回答

试试这个。

def subtract_lists(a, b):
    """ Subtracts two lists. Throws ValueError if b contains items not in a """
    # Terminate if b is empty, otherwise remove b[0] from a and recurse
    return a if len(b) == 0 else [a[:i] + subtract_lists(a[i+1:], b[1:]) 
                                  for i in [a.index(b[0])]][0]

>>> x = [1,2,3,4,5,6,7,8,9,0]
>>> y = [1,3,5,7,9]
>>> subtract_lists(x,y)
[2, 4, 6, 8, 0]
>>> x = [1,2,3,4,5,6,7,8,9,0,9]
>>> subtract_lists(x,y)
[2, 4, 6, 8, 0, 9]     #9 is only deleted once
>>>

使用一个列表推导式来计算差值,同时保持x的原始顺序:

[item for item in x if item not in y]

如果你不需要列表属性(例如,排序),使用一个集差异,正如其他答案所建议的:

list(set(x) - set(y))

为了允许x - y中缀语法,在从list继承的类上重写__sub__:

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   
list1 = ['a', 'c', 'a', 'b', 'k'] 
list2 = ['a', 'a', 'a', 'a', 'b', 'c', 'c', 'd', 'e', 'f'] 
for e in list1: 
    try: 
        list2.remove(e) 
    except ValueError: 
        print(f'{e} not in list') 
list2 
# ['a', 'a', 'c', 'd', 'e', 'f']

这将改变list2。如果你想保护list2,只需复制它,并在这段代码中使用list2的副本。

这个例子减去了两个列表:

# List of pairs of points
list = []
list.append([(602, 336), (624, 365)])
list.append([(635, 336), (654, 365)])
list.append([(642, 342), (648, 358)])
list.append([(644, 344), (646, 356)])
list.append([(653, 337), (671, 365)])
list.append([(728, 13), (739, 32)])
list.append([(756, 59), (767, 79)])

itens_to_remove = []
itens_to_remove.append([(642, 342), (648, 358)])
itens_to_remove.append([(644, 344), (646, 356)])

print("Initial List Size: ", len(list))

for a in itens_to_remove:
    for b in list:
        if a == b :
            list.remove(b)

print("Final List Size: ", len(list))

@aaronasterling提供的答案看起来不错,但是,它与列表的默认接口不兼容:x = MyList(1,2,3,4) vs x = MyList([1,2,3,4])。因此,下面的代码可以用作更友好的python列表:

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