使用另一个字典重新映射递归字典的键

时间:2015-12-14 15:49:01

标签: python dictionary

dict1 = {'ar': {'ad': 'tues',
                'ag': 'mon',
                'az': {'ae': {'ai': {'am': 'yo',
                                     'aq': 'go',
                                     'as': {'al': 'kato',
                                            'aq': 'so',
                                            'as': 'mo',
                                            'ay': 'to'},
                                     'ay': 'wed'},
                              'aq': 'talo'},
                       'aj': {'ai': {'am': 'masan.com',
                                     'ao': 'swan toro',
                                     'ay': 'ryan'},
                              'ax': 'qua-la-sa'},
                       'ap': {'ak': 'stepg-sdd:sdf', 'au': 'asfd.asf'},
                       'at': 'pirat9333',
                       'av': {'ac': {'aa': 'http://swan.com',
                                     'ah': 'ryan',
                                     'am': '1123'},
                              'af': 'Tran swa'},
                       'aw': {'ai': {'aq': 'ejs-33',
                                     'as': {'as': '213bvn', 'ay': 'qwqwa-wnnf'},
                                     'ay': 'mnhfg'},
                              'aq': '1234bfh'}}}}
dict2 = {'key1': 'aa', 'key2': 'ab', 'key3': 'ac', 'key4': 'ad', 'key5': 'ae', 'key6': 'af', 'key7': 'ag', 'key8': 'ah', 'key9': 'ai', 'key10': 'aj', 'key11': 'ak', 'key12': 'al', 'key13': 'am', 'key14': 'an', 'key15': 'ao', 'key16': 'ap', 'key17': 'aq', 'key18': 'ar', 'key2': 'as', 'key19': 'at', 'key20': 'au', 'key21': 'av', 'key22': 'aw', 'key23': 'ax', 'key24': 'ay', 'key25': 'az'}

dict3应将dict1的所有键替换为dict2的键,其中dict1的键= dict2的值。

预期输出为:

dict3 = {'key18': {'key25': {'key10': {'key23': 'qua-la-sa',
                                       'key9': {'key13': 'masan.com',
                                                'key15': 'swan toro',
                                                'key24': 'ryan'}},
                             'key16': {'key11': 'stepg-sdd:sdf', 'key20': 'asfd.asf'},
                             'key19': 'pirat9333',
                             'key21': {'key3': {'key1': 'http://swan.com',
                                                'key13': '1123',
                                                'key8': 'ryan'},
                                       'key6': 'Tran swa'},
                             'key22': {'key17': '1234bfh',
                                       'key9': {'key17': 'ejs-33',
                                                'key2': {'key2': '213bvn',
                                                         'key24': 'qwqwa-wnnf'},
                                                'key24': 'mnhfg'}},
                             'key5': {'key17': 'talo',
                                      'key9': {'key13': 'yo',
                                               'key17': 'go',
                                               'key2': {'key12': 'kato',
                                                        'key17': 'so',
                                                        'key2': 'mo',
                                                        'key24': 'to'},
                                               'key24': 'wed'}}},
                   'key4': 'tues',
                   'key7': 'mon'}}

因为它是一个高度嵌套的字典,我无法获得预期的输出。

1 个答案:

答案 0 :(得分:1)

反转dict2,以便您可以将dict1中的键直接映射到新键:

dict2_inverse = {v: k for k, v in dict2.items()}

然后通过重新映射密钥的其他字典递归:

def recursive_remap(d, keymap):
    if isinstance(d, dict):
        return {keymap[k]: recursive_remap(v, keymap) for k, v in d.items()}
    return d

dict3 = recursive_remap(dict1, dict2_inverse)

演示:

>>> from pprint import pprint
>>> def recursive_remap(d, keymap):
...     if isinstance(d, dict):
...         return {keymap[k]: recursive_remap(v, keymap) for k, v in d.items()}
...     return d
...
>>> dict1 = {'ar': {'az': {'ae': {'aq': 'talo', 'ai': {'aq': 'go', 'ay': 'wed', 'as': {'aq': 'so', 'ay': 'to', 'as': 'mo', 'al': 'kato'}, 'am': 'yo'}}, 'aj': {'ai': {'ay': 'ryan', 'am': 'masan.com', 'ao': 'swan toro'}, 'ax': 'qua-la-sa'}, 'ap': {'ak': 'stepg-sdd:sdf', 'au': 'asfd.asf'}, 'at': 'pirat9333', 'aw': {'aq': '1234bfh', 'ai': {'aq': 'ejs-33', 'ay': 'mnhfg', 'as': {'ay': 'qwqwa-wnnf', 'as': '213bvn'}}}, 'av': {'ac': {'aa': 'http://swan.com', 'ah': 'ryan', 'am': '1123'}, 'af': 'Tran swa'}}, 'ad': 'tues', 'ag': 'mon'}}
>>> dict2 = {'key1': 'aa', 'key2': 'ab', 'key3': 'ac', 'key4': 'ad', 'key5': 'ae', 'key6': 'af', 'key7': 'ag', 'key8': 'ah', 'key9': 'ai', 'key10': 'aj', 'key11': 'ak', 'key12': 'al', 'key13': 'am', 'key14': 'an', 'key15': 'ao', 'key16': 'ap', 'key17': 'aq', 'key18': 'ar', 'key2': 'as', 'key19': 'at', 'key20': 'au', 'key21': 'av', 'key22': 'aw', 'key23': 'ax', 'key24': 'ay', 'key25': 'az'}
>>> dict2_inverse = {v: k for k, v in dict2.items()}
>>> recursive_remap(dict1, dict2_inverse)
{'key18': {'key25': {'key16': {'key11': 'stepg-sdd:sdf', 'key20': 'asfd.asf'}, 'key10': {'key9': {'key13': 'masan.com', 'key24': 'ryan', 'key15': 'swan toro'}, 'key23': 'qua-la-sa'}, 'key5': {'key9': {'key17': 'go', 'key13': 'yo', 'key24': 'wed', 'key2': {'key17': 'so', 'key24': 'to', 'key12': 'kato', 'key2': 'mo'}}, 'key17': 'talo'}, 'key19': 'pirat9333', 'key22': {'key9': {'key17': 'ejs-33', 'key24': 'mnhfg', 'key2': {'key24': 'qwqwa-wnnf', 'key2': '213bvn'}}, 'key17': '1234bfh'}, 'key21': {'key3': {'key1': 'http://swan.com', 'key8': 'ryan', 'key13': '1123'}, 'key6': 'Tran swa'}}, 'key4': 'tues', 'key7': 'mon'}}
>>> pprint(_)
{'key18': {'key25': {'key10': {'key23': 'qua-la-sa',
                               'key9': {'key13': 'masan.com',
                                        'key15': 'swan toro',
                                        'key24': 'ryan'}},
                     'key16': {'key11': 'stepg-sdd:sdf', 'key20': 'asfd.asf'},
                     'key19': 'pirat9333',
                     'key21': {'key3': {'key1': 'http://swan.com',
                                        'key13': '1123',
                                        'key8': 'ryan'},
                               'key6': 'Tran swa'},
                     'key22': {'key17': '1234bfh',
                               'key9': {'key17': 'ejs-33',
                                        'key2': {'key2': '213bvn',
                                                 'key24': 'qwqwa-wnnf'},
                                        'key24': 'mnhfg'}},
                     'key5': {'key17': 'talo',
                              'key9': {'key13': 'yo',
                                       'key17': 'go',
                                       'key2': {'key12': 'kato',
                                                'key17': 'so',
                                                'key2': 'mo',
                                                'key24': 'to'},
                                       'key24': 'wed'}}},
           'key4': 'tues',
           'key7': 'mon'}}