我在python中有2个词典
dict1是一本字典: -
dict1 = 'a':10, 'b':20, 'c':30, 'd':40, 'e':50, 'f':60
dict2是字典词典: -
dict2 = 'A': {'(0,0)':['b','d'], (1,1):['e']}, 'B':{(0,0):['a','c','f']}
我希望输出如下: -
'A' : {'(0,0)':[60], (1,1):[50]} , 'B':{(0,0):[100]}
其中A中的(0,0)的值是来自dict1的总和[b + d]
和B中的(0,0)值是来自dict1的总和[a + c + f]
答案 0 :(得分:1)
一个很酷的衬垫将是:
>>> dict1 = {'a':10, 'b':20, 'c':30, 'd':40, 'e':50, 'f':60}
>>> dict2 = { 'A': {'(0,0)':['b','d'], (1,1):['e']}, 'B':{(0,0):['a','c','f']} }
>>> { k2: { k1: [sum( dict1[item] for item in dict2[k2][k1] )] for k1 in dict2[k2]} for k2 in dict2 }
{'A': {'(0,0)': [60], (1, 1): [50]}, 'B': {(0, 0): [100]}}