3

好吧,假设我有这两个字典:

A = {(3,'x'):-2, (6,'y'):3, (8, 'b'):9}
B = {(3,'y'):4, (6,'y'):6}

我正在尝试将它们添加在一起,以便获得类似于以下内容的字典:

C = {(3,'x'):-2,(3,'y'):4, (6,'y'):9, (8, 'b'):9}

我已经尝试对任何长度的听写做出这样的理解。但是对于新手来说似乎有点困难。我处于尝试这样的水平,例如:

编辑:

>>> {k:A[k]+B[d] for k in A for d in B}
{(6, 'y'): 7, (3, 'x'): 2, (8, 'b'): 13}

由于帮助,我走到了这一步,但由于某种原因它遗漏了 (3,'y'):4

4

7 回答 7

11

我会为此使用collections.Counter

>>> A = {(3,'x'):-2, (6,'y'):3, (8, 'b'):9}
>>> B = {(3,'y'):4, (6,'y'):6}
>>> import collections
>>> C = collections.Counter(A)
>>> C.update(B)
>>> dict(C)
{(3, 'y'): 4, (8, 'b'): 9, (3, 'x'): -2, (6, 'y'): 9}
于 2013-07-24T01:37:54.753 回答
8

由于您使用的是 Python 3,因此一种可能的方法是:

>>> A = {(3,'x'):-2, (6,'y'):3, (8, 'b'):9}
>>> B = {(3,'y'):4, (6,'y'):6}
>>> {k: A.get(k,0) + B.get(k,0) for k in A.keys() | B.keys()}
{(8, 'b'): 9, (3, 'x'): -2, (6, 'y'): 9, (3, 'y'): 4}

在 Python 3 中,.keys()返回一个dict_keys对象,我们可以使用|运算符来取两者的并集。(这就是为什么A.keys() + B.keys()不起作用。)

(我可能会使用Counter我自己,FWIW。)

于 2013-07-24T02:19:52.900 回答
4

遍历来自 A 和 B 的键列表

>>> A = {(3,'x'):-2, (6,'y'):3, (8, 'b'):9}
>>> B = {(3,'y'):4, (6,'y'):6}
>>> C = dict()
>>> for key in set(A.keys() + B.keys()):
...     C[key] = A.get(key, 0) + B.get(key, 0)
... 
>>> C
{(3, 'y'): 4, (8, 'b'): 9, (3, 'x'): -2, (6, 'y'): 9}
于 2013-07-24T01:39:35.827 回答
2

这可能不是最有效的方法,但它更通用。

#!/usr/bin/python
A = {(3,'x'):-2, (6,'y'):3, (8, 'b'):9}
B = {(3,'y'):4, (6,'y'):6}

dd={}
for d in (A, B): #Put as many dictionaries as you would like here
  for key, value in d.iteritems():
    dd[key] = dd.get(key, 0) + value
于 2013-07-24T01:47:48.447 回答
1

我的建议是:

for i in dictA.items():
    if i[0] not in dictA:
        dictB[i[0]] = i[1]
    else:
        dictB[i[0]] = dictB[i[0]] + i[1]

这可能是这里最不花哨的一个

于 2013-07-24T01:45:57.550 回答
1

这是使用理解的解决方案。

A = {(3,'x'):-2, (6,'y'):3, (8, 'b'):9}
B = {(3,'y'):4, (6,'y'):6}

C = {k:A.get(k, 0) + B.get(k, 0) for k in set(A.keys() + B.keys())}

print C
# {(3, 'y'): 4, (8, 'b'): 9, (3, 'x'): -2, (6, 'y'): 9}

这样做是组合键并删除任何重复项。然后它简单地将每个键的值相加。如果 A 或 B 没有其中一个值,则只需从另一个字典中获取值。

于 2013-07-24T02:10:19.513 回答
1
#Needed if you're using python3
from functools import reduce    

A = {(3,'x'):-2, (6,'y'):3, (8, 'b'):9}
B = {(3,'y'):4, (6,'y'):6}

#Merging all the key/value(s) inside one big list
C = list(A.items()) + list(B.items())
#C = [((3,'x'), -2), ((6,'y'), 3), ...]    

#Keeping a list of all unique (hence the set) keys available
keys = set([key[0] for key in C])
#keys = set([(3,'x'), (6,'y'), ...])

result = {}
for key in keys:
    #Extracing the pairs that corresponds to the current key
    local = [item for item in C if item[0] == key]
    #local = [((6,'y'), 3), ((6,'y'), 6)]

    #Actually doing the sum and storing the ready to insert result
    my_sum = reduce(lambda x,y: (x[0], x[1] + y[1]), local)
    #my_sum = [((6,'y'), 9)]

    #Actually inserting the result into the result set
    result.update({my_sum[0]: my_sum[1]})
    #result.update({(6,'y'): 9})

>>> result
{(3, 'y'): 4, (8, 'b'): 9, (3, 'x'): -2, (6, 'y'): 9}
于 2013-07-24T02:14:58.617 回答