python dict.add_by_value(dict_2)?

人走茶凉 提交于 2019-11-27 09:29:05
John La Rooy

Easiest to just use a Counter

>>> from collections import Counter
>>> a = dict(a=1,b=2    )
>>> b = dict(    b=3,c=2)
>>> Counter(a)+Counter(b)
Counter({'b': 5, 'c': 2, 'a': 1})
>>> dict(Counter({'b': 5, 'c': 2, 'a': 1}))
{'a': 1, 'c': 2, 'b': 5}

solving not in terms of "length" but performance, I'd do the following:

>>> from collections import defaultdict
>>> def d_sum(a, b):
        d = defaultdict(int, a)
        for k, v in b.items():
            d[k] += v
        return dict(d)
>>> a = {'a': 1, 'b': 2}
>>> b = {'c': 2, 'b': 3}
>>> d_sum(a, b)
{'a': 1, 'c': 2, 'b': 5}

it's also py3k-compatible, unlike your original code.

In my first impression, I will write:

>>> c = a.copy()
>>> for k in b: c[k] = c.get(k, 0) + b[k]

If you want short code, you're there.

If you want clean code, inherit from Ber's defaultdict and overload __add__:

from collections import defaultdict

class summable(defaultdict):
    def __add__(self, rhs):
        new = summable()
        for i in (self.keys() + rhs.keys()):
            new[i] = self.get(i, 0) + rhs.get(i, 0)
        return new

a = summable(int, a=1, b=2)
b = summable(int, b=3, c=4)
c = a + b
print c

Gives:

>>> 
defaultdict(None, {'a': 1, 'c': 4, 'b': 5})
>>> 

I think one line of code is already pretty short :)

I may become "half a line", it you use defaultdict and remove some unnecessary list and set creations:

from collections import defaultdict

a = defaultdict(int, a=1, b=2)
b = defaultdict(int, b=3, c=4)

c = dict((k, a[k]+b[k]) for k in (a.keys() + b.keys()))
print c

The first thing I think of is a bit more efficient and (IMO) a bit more elegant, but still too much typing. Actually, it's about equivalent to kcwu's.

c = reduce(lambda(d, k): [d.update({k: d.get(k, 0) + b[k]}), d][1], b, a.copy())

It's really a shame that dict.update doesn't return self. I guess it's not the Python way. If it did, the [..., d][1] trick would be unnecessary.


Perl: "Easy things are easy, hard things are possible"

%a = (a => 1, b => 2);
%b = (b => 3, c => 2);

%c = (%a, map {$_ => $a{$_} + $b{$_}} keys %b);

Haskell: "Easy things are hard, hard things are easy" "Hard things are easy, the impossible just happened"

import qualified Data.Map as M

a = M.fromList [('a', 1), ('b', 2)]
b = M.fromList [('b', 3), ('c', 2)]

c = M.unionWith (+) a b
jfs

Comment for @John Pirie's answer:

Here's implementation that doesn't use (self.keys() + rhs.keys()):

from collections import defaultdict

class sumdict(defaultdict):
    def __add__(self, rhs):
        d = self.copy() 
        d += rhs
        return d
    __radd__ = lambda self, lhs: self + lhs
    def __iadd__(self, rhs):
        for k, v in rhs.items():
            self[k] += v
        return self

a = sumdict(int, a=1, b=2)
b = dict(b=3, c=4)
c = b + a
a += b

assert a == c == {'a': 1, 'c': 4, 'b': 5} != b
def GenerateSum():
  for k in set(a).union(b):
    yield k, a.get(k, 0) + b.get(k, 0)

e = dict(GenerateSum())
print e

or, with a one liner:

 print dict((k, a.get(k,0) + b.get(k,0)) for k in set(a).union(b))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!