python dict.add_by_value(dict_2)?

2019-01-09 15:37发布

The problem:

>>> a = dict(a=1,b=2    )
>>> b = dict(    b=3,c=2)

>>> c = ???

c = {'a': 1, 'b': 5, 'c': 2}

So, the idea is two add to dictionaries by int/float values in the shortest form. Here's one solution that I've devised, but I don't like it, cause it's long:

c = dict([(i,a.get(i,0) + b.get(i,0)) for i in set(a.keys()+b.keys())])

I think there must be a shorter/concise solution (maybe something to do with reduce and operator module? itertools?)... Any ideas?


Update: I'm really hoping to find something more elegant like "reduce(operator.add, key = itemgetter(0), a+b)". (Obviously that isn't real code, but you should get the idea). But it seems that may be a dream.


Update: Still loking for more concise solutions. Maybe groupby can help? The solution I've come up with using "reduce"/"groupby" isn't actually concise:

from itertools import groupby
from operator import itemgetter,add

c = dict( [(i,reduce(add,map(itemgetter(1), v))) \
              for i,v in groupby(sorted(a.items()+b.items()), itemgetter(0))] )

8条回答
爷的心禁止访问
2楼-- · 2019-01-09 15:50

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.

查看更多
迷人小祖宗
3楼-- · 2019-01-09 16:00

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
查看更多
戒情不戒烟
4楼-- · 2019-01-09 16:01

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})
>>> 
查看更多
唯我独甜
5楼-- · 2019-01-09 16:03

In my first impression, I will write:

>>> c = a.copy()
>>> for k in b: c[k] = c.get(k, 0) + b[k]
查看更多
混吃等死
6楼-- · 2019-01-09 16:07

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}
查看更多
狗以群分
7楼-- · 2019-01-09 16:07

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
查看更多
登录 后发表回答