添加计数器时如何保留原始字典

时间:2015-07-21 08:57:15

标签: python python-2.7 dictionary counter

根据我的理解,我知道何时调用Counter来隐蔽字典。这个字典包括键的值为零将消失。

from collections import Counter

a = {"a": 1, "b": 5, "d": 0}
b = {"b": 1, "c": 2}

print Counter(a) + Counter(b)

如果我想保留我的钥匙,该怎么办?

这是我的预期结果:

Counter({'b': 6, 'c': 2, 'a': 1, 'd': 0})

4 个答案:

答案 0 :(得分:4)

您也可以使用计数器的update()方法代替+运算符,例如 -

>>> a = {"a": 1, "b": 5, "d": 0}
>>> b = {"b": 1, "c": 2}
>>> x = Counter(a)
>>> x.update(Counter(b))
>>> x
Counter({'b': 6, 'c': 2, 'a': 1, 'd': 0})

update()函数添加计数而不是替换它们,并且它也不会删除零值。我们也可以先Counter(b),然后使用Counter(a)更新,例如 -

>>> y = Counter(b)
>>> y.update(Counter(a))
>>> y
Counter({'b': 6, 'c': 2, 'a': 1, 'd': 0})

答案 1 :(得分:1)

不幸的是,在对两个计数器求和时,只使用具有正计数的元素。

如果要保持元素的计数为零,可以定义如下函数:

def addall(a, b):
    c = Counter(a)          # copy the counter a, preserving the zero elements
    for x in b:             # for each key in the other counter
        c[x] += b[x]        # add the value in the other counter to the first
    return c

答案 2 :(得分:1)

您可以将Counter作为子类并调整其__add__方法:

from collections import Counter


class MyCounter(Counter):
    def __add__(self, other):
        """Add counts from two counters.
        Preserves counts with zero values.

        >>> MyCounter('abbb') + MyCounter('bcc')
        MyCounter({'b': 4, 'c': 2, 'a': 1})
        >>> MyCounter({'a': 1, 'b': 0}) + MyCounter({'a': 2, 'c': 3})
        MyCounter({'a': 3, 'c': 3, 'b': 0})
        """
        if not isinstance(other, Counter):
            return NotImplemented
        result = MyCounter()
        for elem, count in self.items():
            newcount = count + other[elem]
            result[elem] = newcount
        for elem, count in other.items():
            if elem not in self:
                result[elem] = count
        return result


counter1 = MyCounter({'a': 1, 'b': 0})
counter2 = MyCounter({'a': 2, 'c': 3})

print(counter1 + counter2)  # MyCounter({'a': 3, 'c': 3, 'b': 0})

答案 3 :(得分:0)

我帮助Anand S Kumar做更多解释。

即使你的dict包含负值,它仍会保留你的密钥。

from collections import Counter

a = {"a": 1, "b": 5, "d": -1}
b = {"b": 1, "c": 2}

print Counter(a) + Counter(b)
#Counter({'b': 6, 'c': 2, 'a': 1})

x = Counter(a)
x.update(Counter(b))
print x
#Counter({'b': 6, 'c': 2, 'a': 1, 'd': -1})