创建一个结合其他字典python的字典

时间:2017-11-17 06:28:25

标签: python dictionary zip

我有两个类型的词典:

dict1 = {NTID: (ID, CN)}

dict2 = {NTID: INCHI}

我希望得到:

dict3 = {NTID: (ID, CN, INCHI)}

我尝试使用zip,如下所示:

dict_3 = dict(zip(dict1, dict2))

BUt我将NTID作为键和值。请注意,每个字典有多个值,但dict1和dict2之间的值的顺序是一致的

编辑:

dict1的例子是(键:'1-4-HYDROXYPHENYL','4-AMINO-BUTYRATE','ACETYLCHOLINE','ASN')

{'1-4-HYDROXYPHENYL': [],
 '4-AMINO-BUTYRATE': [('24', 'gamma-aminobutyrate (GABA)'),
  ('172', 'alpha-ketoglutarate'),
  ('173', 'succinate'),
  ('399', 'Succinate semialdehyde'),
  ('111', 'putrescine'),
  ('114', '4-guanidinobutanoate'),
  ('115', '4-acetamidobutanoate')],
 'ACETYLCHOLINE': [],
 'ASN': [('1', 'glycine'),
  ('45', 'tyrosine'),
  ('74', 'leucine'),
  ('90', 'methionine'),
  ('80', 'valine')]}

dict2:

{'1-4-HYDROXYPHENYL': [],
 '4-AMINO-BUTYRATE': ['InChI=1S/C4H9NO2/c5-3-1-2-4(6)7/h1-3,5H2,(H,6,7)',
  'InChI=1S/C5H6O5/c6-3(5(9)10)1-2-4(7)8/h1-2H2,(H,7,8)(H,9,10)/p-2',
  'InChI=1S/C4H6O4/c5-3(6)1-2-4(7)8/h1-2H2,(H,5,6)(H,7,8)/p-2',
  'InChI=1S/C4H6O3/c5-3-1-2-4(6)7/h3H,1-2H2,(H,6,7)/p-1',
  'InChI=1S/C4H12N2/c5-3-1-2-4-6/h1-6H2/p+2',
  'InChI=1S/C5H11N3O2/c6-5(7)8-3-1-2-4(9)10/h1-3H2,(H,9,10)(H4,6,7,8)',
  'InChI=1S/C6H11NO3/c1-5(8)7-4-2-3-6(9)10/h2-4H2,1H3,(H,7,8)(H,9,10)/p-1'],
 'ACETYLCHOLINE': [],
 'ASN': ['InChI=1S/C9H11NO3',
  'InChI=1S/C9H11NO2/c10-8',
  'InChI=1S/C3H7NO2/c1-2(4)3(5)6',
  'InChI=1S/C5H9NO2/c7-5(8)4-2-1-3',
  'InChI=1S/C4H9NO3/c1-2(6)3',
  'InChI=1S/C4H8N2O3/c5-2(4(8)',
  'InChI=1S/C6H13NO2/c1-3-4(2)',
  'InChI=1S/C6H9N3O2/c7-5(6(10)',
  'InChI=1S/C4H7NO4/c5-2(4(8)9)',
  'InChI=1S/C4H4O5/c5-2(4(8)9)1-',
  'InChI=1S/C3H7NO2S/c4-2(1-7)3(5)',
  'InChI=1S/C5H10N2O3/c6-3(5(9)'}

dict3(输出)应如下所示:

{'1-4-HYDROXYPHENYL': [],
 '4-AMINO-BUTYRATE': [('24', 'gamma-aminobutyrate (GABA)', 'InChI=1S/C4H9NO2/c5-3-1-2-4(6)7/h1-3,5H2,(H,6,7)'),
  ('172', 'alpha-ketoglutarate', 'InChI=1S/C5H6O5/c6-3(5(9)10)1-2-4(7)8/h1-2H2,(H,7,8)(H,9,10)/p-2', 'InChI=1S/C4H6O4/c5-3(6)1-2-4(7)8/h1-2H2,(H,5,6)(H,7,8)/p-2'),
  ('173', 'succinate', 'InChI=1S/C4H6O3/c5-3-1-2-4(6)7/h3H,1-2H2,(H,6,7)/p-1'),
  etc...

3 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

dict1 = {'NTID': ('ID', 'CN')}

dict2 = {'NTID': 'INCHI'}


    final_dict={}
    for key,value in dict1.items():
        final_dict[key]=[value[0],value[1]]
        if key in dict2:
            final_dict[key].append(dict2.get(key))

    print(final_dict)

输出:

{'NTID': ['ID', 'CN', 'INCHI']}

如果你想要显示元组,那么:

final_dict={}
for key,value in dict1.items():
    final_dict[key]=value
    if key in dict2:
        final_dict[key]+=(dict2.get(key),)

print(final_dict)

输出:

{'NTID': ('ID', 'CN', 'INCHI')}

第二,你可以使用defaultdict:

dict1 = {'NTID': ('ID', 'CN')}

dict2 = {'NTID': 'INCHI'}

from collections import defaultdict

d=defaultdict(list)
for item in (dict1,dict2):
    for key,value in item.items():
        d[key].append(value)

print(d)
  

根据问题编辑更新:

dict_1={'1-4-HYDROXYPHENYL': [],
 '4-AMINO-BUTYRATE': [('24', 'gamma-aminobutyrate (GABA)'),
  ('172', 'alpha-ketoglutarate'),
  ('173', 'succinate'),
  ('399', 'Succinate semialdehyde'),
  ('111', 'putrescine'),
  ('114', '4-guanidinobutanoate'),
  ('115', '4-acetamidobutanoate')],
 'ACETYLCHOLINE': [],
 'ASN': [('1', 'glycine'),
  ('45', 'tyrosine'),
  ('74', 'leucine'),
  ('90', 'methionine'),
  ('80', 'valine')]}



dict_2={'1-4-HYDROXYPHENYL': [],
 '4-AMINO-BUTYRATE': ['InChI=1S/C4H9NO2/c5-3-1-2-4(6)7/h1-3,5H2,(H,6,7)',
  'InChI=1S/C5H6O5/c6-3(5(9)10)1-2-4(7)8/h1-2H2,(H,7,8)(H,9,10)/p-2',
  'InChI=1S/C4H6O4/c5-3(6)1-2-4(7)8/h1-2H2,(H,5,6)(H,7,8)/p-2',
  'InChI=1S/C4H6O3/c5-3-1-2-4(6)7/h3H,1-2H2,(H,6,7)/p-1',
  'InChI=1S/C4H12N2/c5-3-1-2-4-6/h1-6H2/p+2',
  'InChI=1S/C5H11N3O2/c6-5(7)8-3-1-2-4(9)10/h1-3H2,(H,9,10)(H4,6,7,8)',
  'InChI=1S/C6H11NO3/c1-5(8)7-4-2-3-6(9)10/h2-4H2,1H3,(H,7,8)(H,9,10)/p-1'],
 'ACETYLCHOLINE': [],
 'ASN': ['InChI=1S/C9H11NO3',
  'InChI=1S/C9H11NO2/c10-8',
  'InChI=1S/C3H7NO2/c1-2(4)3(5)6',
  'InChI=1S/C5H9NO2/c7-5(8)4-2-1-3',
  'InChI=1S/C4H9NO3/c1-2(6)3',
  'InChI=1S/C4H8N2O3/c5-2(4(8)',
  'InChI=1S/C6H13NO2/c1-3-4(2)',
  'InChI=1S/C6H9N3O2/c7-5(6(10)',
  'InChI=1S/C4H7NO4/c5-2(4(8)9)',
  'InChI=1S/C4H4O5/c5-2(4(8)9)1-',
  'InChI=1S/C3H7NO2S/c4-2(1-7)3(5)',
  'InChI=1S/C5H10N2O3/c6-3(5(9)']}


final_dict={}
for key,value in dict_1.items():
    if key in dict_2:
        for zip_value in zip(value,dict_2.get(key)):
            if key not in final_dict:
                final_dict[key]=[zip_value]
            else:
                final_dict[key].append(zip_value)



print(final_dict)

输出:

{'ASN': [(('1', 'glycine'), 'InChI=1S/C9H11NO3'), (('45', 'tyrosine'), 'InChI=1S/C9H11NO2/c10-8'), (('74', 'leucine'), 'InChI=1S/C3H7NO2/c1-2(4)3(5)6'), (('90', 'methionine'), 'InChI=1S/C5H9NO2/c7-5(8)4-2-1-3'), (('80', 'valine'), 'InChI=1S/C4H9NO3/c1-2(6)3')], '4-AMINO-BUTYRATE': [(('24', 'gamma-aminobutyrate (GABA)'), 'InChI=1S/C4H9NO2/c5-3-1-2-4(6)7/h1-3,5H2,(H,6,7)'), (('172', 'alpha-ketoglutarate'), 'InChI=1S/C5H6O5/c6-3(5(9)10)1-2-4(7)8/h1-2H2,(H,7,8)(H,9,10)/p-2'), (('173', 'succinate'), 'InChI=1S/C4H6O4/c5-3(6)1-2-4(7)8/h1-2H2,(H,5,6)(H,7,8)/p-2'), (('399', 'Succinate semialdehyde'), 'InChI=1S/C4H6O3/c5-3-1-2-4(6)7/h3H,1-2H2,(H,6,7)/p-1'), (('111', 'putrescine'), 'InChI=1S/C4H12N2/c5-3-1-2-4-6/h1-6H2/p+2'), (('114', '4-guanidinobutanoate'), 'InChI=1S/C5H11N3O2/c6-5(7)8-3-1-2-4(9)10/h1-3H2,(H,9,10)(H4,6,7,8)'), (('115', '4-acetamidobutanoate'), 'InChI=1S/C6H11NO3/c1-5(8)7-4-2-3-6(9)10/h2-4H2,1H3,(H,7,8)(H,9,10)/p-1')]}
  

如果你不想要那些额外的括号,很少修改:

final_dict={}
for key,value in dict_1.items():
    if key in dict_2:
        for zip_value in zip(value,dict_2.get(key)):

            if key not in final_dict:
                final_dict[key]=[zip_value[0][0],zip_value[0][1],zip_value[1]]
            else:
                final_dict[key].append((zip_value[0][0],zip_value[0][1],zip_value[1]))



print(final_dict)

输出:

{'ASN': ['1', 'glycine', 'InChI=1S/C9H11NO3', ('45', 'tyrosine', 'InChI=1S/C9H11NO2/c10-8'), ('74', 'leucine', 'InChI=1S/C3H7NO2/c1-2(4)3(5)6'), ('90', 'methionine', 'InChI=1S/C5H9NO2/c7-5(8)4-2-1-3'), ('80', 'valine', 'InChI=1S/C4H9NO3/c1-2(6)3')], '4-AMINO-BUTYRATE': ['24', 'gamma-aminobutyrate (GABA)', 'InChI=1S/C4H9NO2/c5-3-1-2-4(6)7/h1-3,5H2,(H,6,7)', ('172', 'alpha-ketoglutarate', 'InChI=1S/C5H6O5/c6-3(5(9)10)1-2-4(7)8/h1-2H2,(H,7,8)(H,9,10)/p-2'), ('173', 'succinate', 'InChI=1S/C4H6O4/c5-3(6)1-2-4(7)8/h1-2H2,(H,5,6)(H,7,8)/p-2'), ('399', 'Succinate semialdehyde', 'InChI=1S/C4H6O3/c5-3-1-2-4(6)7/h3H,1-2H2,(H,6,7)/p-1'), ('111', 'putrescine', 'InChI=1S/C4H12N2/c5-3-1-2-4-6/h1-6H2/p+2'), ('114', '4-guanidinobutanoate', 'InChI=1S/C5H11N3O2/c6-5(7)8-3-1-2-4(9)10/h1-3H2,(H,9,10)(H4,6,7,8)'), ('115', '4-acetamidobutanoate', 'InChI=1S/C6H11NO3/c1-5(8)7-4-2-3-6(9)10/h2-4H2,1H3,(H,7,8)(H,9,10)/p-1')]}

答案 1 :(得分:0)

你可以试试这个:

dict3 = {name: [(*a, b) for a, b in zip(v, dict2[name])]
    for name, v in dict1.items()
}

据我所知,dict1dict2具有相同的键和结构,但存储与同一事物相关的不同信息,并且您希望将它们合并为一个共同的{{1} }}

答案 2 :(得分:0)

dict3 = dict(zip(dict1.keys(), zip(dict1.values(),dict2.values())))