views:

328

answers:

7

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))] )
+3  A: 

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
Ber
+3  A: 

In my first impression, I will write:

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

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})
>>>
John Pirie
`self.keys()` is unnecessary here.
J.F. Sebastian
@J.F., I think it is necessary, but thanks for raising the issue -- my original code snip was flat wrong. Fixed and tested.
John Pirie
@John Pirie: I've posted variant without `self.keys()` http://stackoverflow.com/questions/877295/python-dict-addbyvaluedict2/1027258#1027258
J.F. Sebastian
+5  A: 

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.

SilentGhost
Hmm, that's news for me. What did they change in Py3k that makes my code incompatible?
Slava N
`a.keys() + b.keys()` - you would need to first convert each of them to lists for you code to work
SilentGhost
>>> type(dict().keys()) == <type 'list'>; Isn't dict's keys() the list?
Slava N
not in py3k: it's just a dict view, as are .values() and .items()
SilentGhost
A: 
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))
odwl
+1  A: 

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
ephemient
A: 

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
J.F. Sebastian
Thanks for posting that -- I obviously have a lot more Python to learn.
John Pirie