减去并添加两个列表,而不用在Python中更改它们的顺序

时间:2011-07-31 13:26:20

标签: python list addition subtraction

如果我有列表[68,31,93,35,10](所有数字都不同)和列表[93,0,22,10,99,33,21,9](同样,所有数字都不同,但可能与其他列表重叠),我需要能够准确地获得[68,31,93,35,10,0,22,99,33,21,9],其中第二个列表被附加到第一个列表而没有重复。我还需要能够准确地获得[68,31,35],其中第一个列表在第二个列表中删除了所有重复项。输出始终应与输入的顺序相同。我该怎么做? (如果简单的话,一个衬垫会很好。)

6 个答案:

答案 0 :(得分:3)

l1 = [68, 31, 93, 35,10]
l2 = [93, 0, 22, 10, 99, 33, 21,9]

l1 + [x for x in l2 if not x in l1]
# [68, 31, 93, 35, 10, 0, 22, 99, 33, 21, 9]

[x for x in l1 if not x in l2]
# [68, 31, 35]

编辑:对于长列表,您不希望进行所有这些列表查找。这是另外两个食谱:

联合:

from collections import OrderedDict
OrderedDict().fromkeys(l1+l2).keys()
# [68, 31, 93, 35, 10, 0, 22, 99, 33, 21, 9]

差:

s = set(l2)
[x for x in l1 if not x in s]
# [68, 31, 35]

答案 1 :(得分:1)

假设输入l1l2,您可以使用以下方式计算其有序联合:

l1 + filter(lambda x: x not in l1, l2)

要获得有序差值l1 - l2,请写

filter(lambda x: x not in l2, l1)

或者,使用列表推导:

>>> l1 = [68,31,93,35,10]
>>> l2 = [93,0,22,10,99,33,21,9]
>>> l1 + [el2 for el2 in l2 if el2 not in l1]
[68, 31, 93, 35, 10, 0, 22, 99, 33, 21, 9]
>>> [el1 for el1 in l1 if el1 not in l2]
[68, 31, 35]

如果您使用非常大的列表执行此操作(性能是个问题),请构建set以加快查找速度:

>>> sl1 = set(s1)
>>> l1 + [el2 for el2 in l2 if el2 not in sl1]
[68, 31, 93, 35, 10, 0, 22, 99, 33, 21, 9]
>>> sl2 = set(s2)
>>> [el1 for el1 in l1 if el1 not in sl2]
[68, 31, 35]

答案 2 :(得分:0)

def unique_chain(*iters):
    seen = set()
    for it in iters:
        for item in it:
            if item not in seen:
                yield item
                seen.add(item)

print list(unique_chain([68, 31, 93, 35,10], [93, 0, 22, 10, 99, 33, 21,9]))

答案 3 :(得分:0)

>>> a = [68,31,93,35,10]
>>> b = [93,0,22,10,99,33,21,9]
>>> result= []
>>> temp = a + b
>>> [result.append(x) for x in temp if x not in result]
>>> result
    [68, 31, 93, 35, 10, 0, 22, 99, 33, 21, 9]
>>> a = set(a)
>>> b = set(b)
>>> a - b
    set([35, 68, 31])

答案 4 :(得分:0)

也许你可以使用OrderedSet

import collections

class OrderedSet(collections.MutableSet):
    def __init__(self, iterable, *args, **kwargs):
        super(OrderedSet, self).__init__(*args, **kwargs)
        self._data = collections.OrderedDict()
        self.update(iterable)

    def update(self, iterable):
        self._data.update((x, None) for x in iterable)

    def __iter__(self):
        return iter(self._data)

    def __contains__(self, value):
        return value in self._data

    def __len__(self):
        return len(self._data)

    def __le__(self, other):
        if isinstance(other, OrderedSet):
            return self._data <= other._data
        return super(OrderedSet, self).__le__(other)

    def __and__(self, other):
        # Overrided by make the order of self the preferred one
        if isinstance(other, collections.Set):
            return self._from_iterable(value for value in self 
                                             if value in other)
        return self & set(other)

    def __ior__(self, other):
        self.update(other)
        return self

    def add(self, value):
        self._data[value] = None

    def discard(self, value):
        self._data.pop(value, None)

    def __repr__(self):
        return "%s(%r)" % (type(self).__name__, self._data.keys())

答案 5 :(得分:0)

在定义前两个列表后,

a = [68,31,93,35,10]
b = [93,0,22,10,99,33,21,9]

这是第一个问题的单行解决方案,

c = [x for x in a+b if x not in set(a).intersection(set(b))]

和第二个问题的单线,

d = [x for x in a+b if x not in b]