【问题标题】:How to keep my original dict when doing addition of Counter添加计数器时如何保留我的原始字典
【发布时间】:2015-07-21 08:57:14
【问题描述】:

据我了解,我知道何时调用 Counter 来隐藏字典。此 dict 包含的键值为零将消失。

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})

【问题讨论】:

    标签: python python-2.7 dictionary counter


    【解决方案1】:

    也可以使用Counter的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})
    

    【讨论】:

      【解决方案2】:

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

      如果你想保持元素的计数为零,你可以定义一个这样的函数:

      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
      

      【讨论】:

        【解决方案3】:

        您可以只继承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})
        

        【讨论】:

          【解决方案4】:

          我帮助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})
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-18
            • 2012-08-27
            • 2017-10-26
            • 2018-08-19
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多