用不同的键合并python中的两个字典

2024-05-29 05:19:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两本字典:

dict_conv
Out[162]: 
{'Alanine, aspartate and glutamate metabolism': 4,
 'Ascorbate and aldarate metabolism': 4,
 'Benzoate degradation': 6,
 'Biosynthesis of antibiotics': 16}

dict_org
Out[163]: 
{'Amino sugar and nucleotide sugar metabolism': 5,
 'Arginine and proline metabolism': 4,
 'Biosynthesis of antibiotics': 11,
 'Biosynthesis of secondary metabolites': 21}

我想把它们结合起来。 我喜欢这样:

def mergeDict(dict_conv, dict_org):
   ''' Merge dictionaries and keep values of common keys in list'''
   dict3 = {**dict_conv, **dict_org}
   for key, value in dict3.items():
       if key in dict_conv and key in dict_org:
               dict3[key] = [value , dict_conv[key]]
   return dict3

dict3 = mergeDict(dict_conv, dict_org)

得到了这条命令:

dict3
Out[164]: 
{'Alanine, aspartate and glutamate metabolism': 4,
 'Ascorbate and aldarate metabolism': 4,
 'Benzoate degradation': 6,
 'Biosynthesis of antibiotics': [11, 16],
 'Biosynthesis of secondary metabolites': [21, 26]}

我希望每个键都有两个值(在列表中)。 如果键不在其中一个字典中,我希望它写0,如下所示:

dict3
    Out[164]: 
    {'Alanine, aspartate and glutamate metabolism': [4,0],
     'Amino sugar and nucleotide sugar metabolism': [0,5],
     'Ascorbate and aldarate metabolism': [4,0],
     'Benzoate degradation': [6,0],
     'Biosynthesis of antibiotics': [11, 16],
     'Biosynthesis of secondary metabolites': [0,21]}

(第一个值表示“dict_conv”值,第二个值表示“dict_org”值)

我应该向mergeDict函数添加什么

谢谢:)


Tags: andofkeyinorgsugaroutdict
2条回答

您也可以通过以下方式无条件设置该值:

from pprint import pprint

...

for key in dict3.keys():
    dict3[key] = [dict_conv.get(key, 0), dict_org.get(key, 0)]
....

pprint(dict3)
{'Alanine, aspartate and glutamate metabolism': [4, 0],
 'Amino sugar and nucleotide sugar metabolism': [0, 5],
 'Arginine and proline metabolism': [0, 4],
 'Ascorbate and aldarate metabolism': [4, 0],
 'Benzoate degradation': [6, 0],
 'Biosynthesis of antibiotics': [16, 11],
 'Biosynthesis of secondary metabolites': [0, 21]}

您只需构建一组所有键,然后使用dicts的get方法创建列表,默认值为0:

def merge(d1, d2):
    keys = set(d1.keys()) | set(d2.keys())
    return {key: [d1.get(key, 0), d2.get(key, 0)] for key in keys}

正如@Stephan B所建议的,第一行可以用更短的方式书写:

keys = set(d1) | set(d2)

因为在dict上迭代会在其键上迭代,或者作为:

keys = set(d1).union(d2)

因为unionintersection等的参数只需要是一个iterable,而不一定是一个set

使用您的数据运行示例:

d1 = {'Alanine, aspartate and glutamate metabolism': 4,
     'Ascorbate and aldarate metabolism': 4,
     'Benzoate degradation': 6,
     'Biosynthesis of antibiotics': 16}

d2 = {'Amino sugar and nucleotide sugar metabolism': 5,
     'Arginine and proline metabolism': 4,
     'Biosynthesis of antibiotics': 11,
     'Biosynthesis of secondary metabolites': 21}

print(merge(d1, d2))

输出:

{'Biosynthesis of antibiotics': [16, 11], 
'Amino sugar and nucleotide sugar metabolism': [0, 5], 
'Arginine and proline metabolism': [0, 4], 
'Alanine, aspartate and glutamate metabolism': [4, 0], 
'Ascorbate and aldarate metabolism': [4, 0], 
'Benzoate degradation': [6, 0], 
'Biosynthesis of secondary metabolites': [0, 21]}

相关问题 更多 >

    热门问题