在Python中减去两个列表

时间:2010-01-15 09:53:18

标签: python list collections

在Python中,如何减去两个非唯一的无序列表?假设我们有a = [0,1,2,1,0]b = [0, 1, 1]我想做c = a - b之类的事情并c [2, 0][0, 2]订单不对我来说很重要如果a不包含b中的所有元素,则应抛出异常。

注意这与集合不同!我对找到a和b中元素集的差异不感兴趣,我对实际元素集合之间的差异感兴趣a和b。

我可以用for循环来做这个,在a中查找b的第一个元素,然后从a中删除元素,然后从a中删除等等。但是这对我没有吸引力,它会非常低效(顺序) O(n^2)时间的O(n log n)时间,但在{{1}}时间内执行此操作应该没问题。

13 个答案:

答案 0 :(得分:55)

我知道“for”不是你想要的,但它简单明了:

for x in b:
  a.remove(x)

如果b的成员可能不在a,请使用:

for x in b:
  if x in a:
    a.remove(x)

答案 1 :(得分:29)

Python 2.7和3.2将添加collections.Counter类,这是一个将元素映射到元素出现次数的字典。这可以用作多重集。

根据文档,你应该可以做这样的事情(未经测试,因为我没有安装任何版本)。

from collections import Counter
a = Counter(0,1,2,1)
b = Counter(0,1,1)

print a - b  # ignores items in b missing in a

# check every element in a is in b
# a[key] returns 0 if key not in a, instead of raising an exception
assert all(a[key] > b[key] for key in b)

修改

由于你坚持使用2.5,你可以尝试导入它并定义你自己的版本,如果失败了。这样一来,如果没有,你肯定会得到最新版本,如果没有,你会回到工作版本。如果将来转换为C实现,您也将受益于速度改进。

try:
   from collections import Counter
except ImportError:
    class Counter(dict):
       ...

您可以找到当前的Python源here

答案 2 :(得分:28)

我会以一种更简单的方式做到这一点:

a_b = [e for e in a if not e in b ]

<击>

..正如Wich写的那样,这是错误的 - 只有当项目在列表中是唯一的时才有效。如果是,最好使用

a_b = list(set(a) - set(b))

答案 3 :(得分:6)

我不确定for循环的异议是什么:Python中没有多重集,所以你不能使用内置容器来帮助你。

在我看来,任何一行(如果可能的话)都可能很难理解。寻求可读性和KISS。 Python不是C:)

答案 4 :(得分:5)

Python 2.7+和3.0有collections.Counter(a.k.a。multiset)。文档链接到Python {的Recipe 576611: Counter class

from operator import itemgetter
from heapq import nlargest
from itertools import repeat, ifilter

class Counter(dict):
    '''Dict subclass for counting hashable objects.  Sometimes called a bag
    or multiset.  Elements are stored as dictionary keys and their counts
    are stored as dictionary values.

    >>> Counter('zyzygy')
    Counter({'y': 3, 'z': 2, 'g': 1})

    '''

    def __init__(self, iterable=None, **kwds):
        '''Create a new, empty Counter object.  And if given, count elements
        from an input iterable.  Or, initialize the count from another mapping
        of elements to their counts.

        >>> c = Counter()                           # a new, empty counter
        >>> c = Counter('gallahad')                 # a new counter from an iterable
        >>> c = Counter({'a': 4, 'b': 2})           # a new counter from a mapping
        >>> c = Counter(a=4, b=2)                   # a new counter from keyword args

        '''        
        self.update(iterable, **kwds)

    def __missing__(self, key):
        return 0

    def most_common(self, n=None):
        '''List the n most common elements and their counts from the most
        common to the least.  If n is None, then list all element counts.

        >>> Counter('abracadabra').most_common(3)
        [('a', 5), ('r', 2), ('b', 2)]

        '''        
        if n is None:
            return sorted(self.iteritems(), key=itemgetter(1), reverse=True)
        return nlargest(n, self.iteritems(), key=itemgetter(1))

    def elements(self):
        '''Iterator over elements repeating each as many times as its count.

        >>> c = Counter('ABCABC')
        >>> sorted(c.elements())
        ['A', 'A', 'B', 'B', 'C', 'C']

        If an element's count has been set to zero or is a negative number,
        elements() will ignore it.

        '''
        for elem, count in self.iteritems():
            for _ in repeat(None, count):
                yield elem

    # Override dict methods where the meaning changes for Counter objects.

    @classmethod
    def fromkeys(cls, iterable, v=None):
        raise NotImplementedError(
            'Counter.fromkeys() is undefined.  Use Counter(iterable) instead.')

    def update(self, iterable=None, **kwds):
        '''Like dict.update() but add counts instead of replacing them.

        Source can be an iterable, a dictionary, or another Counter instance.

        >>> c = Counter('which')
        >>> c.update('witch')           # add elements from another iterable
        >>> d = Counter('watch')
        >>> c.update(d)                 # add elements from another counter
        >>> c['h']                      # four 'h' in which, witch, and watch
        4

        '''        
        if iterable is not None:
            if hasattr(iterable, 'iteritems'):
                if self:
                    self_get = self.get
                    for elem, count in iterable.iteritems():
                        self[elem] = self_get(elem, 0) + count
                else:
                    dict.update(self, iterable) # fast path when counter is empty
            else:
                self_get = self.get
                for elem in iterable:
                    self[elem] = self_get(elem, 0) + 1
        if kwds:
            self.update(kwds)

    def copy(self):
        'Like dict.copy() but returns a Counter instance instead of a dict.'
        return Counter(self)

    def __delitem__(self, elem):
        'Like dict.__delitem__() but does not raise KeyError for missing values.'
        if elem in self:
            dict.__delitem__(self, elem)

    def __repr__(self):
        if not self:
            return '%s()' % self.__class__.__name__
        items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
        return '%s({%s})' % (self.__class__.__name__, items)

    # Multiset-style mathematical operations discussed in:
    #       Knuth TAOCP Volume II section 4.6.3 exercise 19
    #       and at http://en.wikipedia.org/wiki/Multiset
    #
    # Outputs guaranteed to only include positive counts.
    #
    # To strip negative and zero counts, add-in an empty counter:
    #       c += Counter()

    def __add__(self, other):
        '''Add counts from two counters.

        >>> Counter('abbb') + Counter('bcc')
        Counter({'b': 4, 'c': 2, 'a': 1})


        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem in set(self) | set(other):
            newcount = self[elem] + other[elem]
            if newcount > 0:
                result[elem] = newcount
        return result

    def __sub__(self, other):
        ''' Subtract count, but keep only results with positive counts.

        >>> Counter('abbbc') - Counter('bccd')
        Counter({'b': 2, 'a': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem in set(self) | set(other):
            newcount = self[elem] - other[elem]
            if newcount > 0:
                result[elem] = newcount
        return result

    def __or__(self, other):
        '''Union is the maximum of value in either of the input counters.

        >>> Counter('abbb') | Counter('bcc')
        Counter({'b': 3, 'c': 2, 'a': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        _max = max
        result = Counter()
        for elem in set(self) | set(other):
            newcount = _max(self[elem], other[elem])
            if newcount > 0:
                result[elem] = newcount
        return result

    def __and__(self, other):
        ''' Intersection is the minimum of corresponding counts.

        >>> Counter('abbb') & Counter('bcc')
        Counter({'b': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        _min = min
        result = Counter()
        if len(self) < len(other):
            self, other = other, self
        for elem in ifilter(self.__contains__, other):
            newcount = _min(self[elem], other[elem])
            if newcount > 0:
                result[elem] = newcount
        return result


if __name__ == '__main__':
    import doctest
    print doctest.testmod()

然后你可以写

 a = Counter([0,1,2,1,0])
 b = Counter([0, 1, 1])
 c = a - b
 print list(c.elements())  # [0, 2]

答案 5 :(得分:4)

使用列表理解:

[i for i in a if not i in b or b.remove(i)]

会做到这一点。但它会在这个过程中改变b。但我同意jkp和Dyno Fu的观点,即使用for循环会更好。

也许有人可以创建一个更好的例子,使用列表理解但仍然是KISS?

答案 6 :(得分:2)

为了证明jkp的观点,'在一条线上的任何东西都可能是非常复杂的理解',我创造了一个单行。请不要模仿我,因为我知道这不是你应该实际使用的解决方案。这只是为了示范目的。

我们的想法是逐个添加值,只要您添加该值的总次数小于此值的总次数减去它在b中的次数:

[ value for counter,value in enumerate(a) if a.count(value) >= b.count(value) + a[counter:].count(value) ]

恐怖!但也许有人可以改进它?它甚至没有bug吗?

编辑:看到Devin Jeanpierre关于使用字典数据结构的评论,我想出了这个oneliner:

sum([ [value]*count for value,count in {value:a.count(value)-b.count(value) for value in set(a)}.items() ], [])

更好,但仍然不可读。

答案 7 :(得分:0)

您可以尝试这样的事情:

class mylist(list):

    def __sub__(self, b):
        result = self[:]
        b = b[:]
        while b:
            try:
                result.remove(b.pop())
            except ValueError:
                raise Exception("Not all elements found during subtraction")
        return result


a = mylist([0, 1, 2, 1, 0] )
b = mylist([0, 1, 1])

>>> a - b
[2, 0]

你必须定义[1,2,3] - [5,6]应输出的内容,我想你想要[1,2,3]这就是为什么我忽略了ValueError。

编辑: 现在我看到如果a不包含所有元素,则需要一个例外,添加它而不是传递ValueError。

答案 8 :(得分:0)

我试图找到一个更优雅的解决方案,但我能做的最好的事情与Dyno Fu所说的基本相同:

from copy import copy

def subtract_lists(a, b):
    """
    >>> a = [0, 1, 2, 1, 0]
    >>> b = [0, 1, 1]
    >>> subtract_lists(a, b)
    [2, 0]

    >>> import random
    >>> size = 10000
    >>> a = [random.randrange(100) for _ in range(size)]
    >>> b = [random.randrange(100) for _ in range(size)]
    >>> c = subtract_lists(a, b)
    >>> assert all((x in a) for x in c)
    """
    a = copy(a)
    for x in b:
        if x in a:
            a.remove(x)
    return a

答案 9 :(得分:0)

这是一个相对较长但有效且易读的解决方案。它是O(n)。

def list_diff(list1, list2):
    counts = {}
    for x in list1:
        try:
            counts[x] += 1
        except:
            counts[x] = 1
    for x in list2:
        try:
            counts[x] -= 1
            if counts[x] < 0:
                raise ValueError('All elements of list2 not in list2')
        except:
            raise ValueError('All elements of list2 not in list1') 
    result = []
    for k, v in counts.iteritems():
        result += v*[k] 
    return result

a = [0, 1, 1, 2, 0]
b = [0, 1, 1]
%timeit list_diff(a, b)
%timeit list_diff(1000*a, 1000*b)
%timeit list_diff(1000000*a, 1000000*b)
100000 loops, best of 3: 4.8 µs per loop
1000 loops, best of 3: 1.18 ms per loop
1 loops, best of 3: 1.21 s per loop

答案 10 :(得分:0)

您可以使用map构造来执行此操作。它看起来很不错,但请注意map行本身将返回None的列表。

a = [1, 2, 3]
b = [2, 3]

map(lambda x:a.remove(x), b)
a

答案 11 :(得分:-3)

c = [i for i in b if i not in a]

答案 12 :(得分:-4)

list(set([x for x in a if x not in b]))
  • 离开ab
  • 是一套独特的“a - b”。
  • 完成。