Home > database >  Create new dict from values in a dict and values in nested dict
Create new dict from values in a dict and values in nested dict

Time:12-13

I want to creat new dict by values from 2 dict: dict1 and dict2

dict1 = {'Jame': {'ID':'158', 
                  'Age':'28', 
                  'Tall':'5.11',
                   ..., 
                  'Point': [{'IELTS':'525', 
                             'SAT':'700', 
                             'TOEFL':'500',
                              ...],
                  'Hair':'Blond',
                  'Gender':'Male',
                ...},
    'Caroline': {'ID':'135', 
                  'Age':'26', 
                  'Tall':'5.7',
                   ..., 
                  'Point': [{'IELTS':'655', 
                             'SAT':'700', 
                             'TOEFL':'500',
                              ...],
                  'Hair':'Black',
                  'Gender':'Female',
                   ...}}

and

dict2 = {'Age'='A1','Tall'='A2','Point'='A3','IELTS'='B1','SAT'='B2','TOEFL'='B3'}

I want to creat new dict by values of dict1 and dict2. I would like Output:

new = {'Jame': {'ID':'158', 
                'A1':'28', 
                'A2':'5.11',
                   ..., 
                'A3': [{'B1':'525', 
                        'B2':'700', 
                        'B3':'500',
                         ...],
                'Hair':'Blond',
                'Gender':'Male',
                ...},
  'Caroline': {'ID':'135', 
                'A1':'26', 
                'A2':'5.7',
                 ..., 
                'A3': [{'B1':'655', 
                        'B2':'700', 
                        'B3':'500',
                        ...],
                'Hair':'Black',
                'Gender':'Female',
                ...}}

I would try some code but it's not working

newdict = dict(zip(dict2.values(), dict1.values()))

or

newdict = {({v for k,v in dict2.items()} if key in dict2.keys() else key):value for key, value in dict1.items()}

or

newdict = {dict1.get(key, dict2[key]):key for key in dict2}

CodePudding user response:

You can do this:

import ast

dict1 = {'Jame': {'ID': '158',
                  'Age': '28',
                  'Tall': '5.11',
                  'Point': [{'IELTS': '525',
                             'SAT': '700',
                             'TOEFL': '500'}],
                  'Hair': 'Blond',
                  'Gender': 'Male'},
         'Caroline': {'ID': '135',
                      'Age': '26',
                      'Tall': '5.7',
                      'Point': [{'IELTS': '655',
                                 'SAT': '700',
                                 'TOEFL': '500'}],
                      'Hair': 'Black',
                      'Gender': 'Female'}}

dict2 = {'Age': 'A1', 'Tall': 'A2', 'Point': 'A3', 'IELTS': 'B1', 'SAT': 'B2', 'TOEFL': 'B3'}

string_dict = str(dict1)

for k, v in dict2.items():
    string_dict = string_dict.replace(k, v)

dict1 = ast.literal_eval(string_dict)
  • Related