简体   繁体   中英

Using dict keys in python as values in a different dict

I would like to create a "translator" type of dict that would assign values that are keys in different dicts, which are nested, to keys in a dict that I created. The problem I run into is that I can't create a value that represents a nested dict key without having to convert that to a string or some other data type, and when I try to use a string as an index to the nested dict, I get an index error. Ideally, my dict would look something like this:

new_dict{
    "new_key_1" : ['subdict1']['subdict2']['old_key_1'],
    "new_key_2" : ['subdict1']['subdict2']['old_key_2'],
    "new_key_3" : ['subdict1']['subdict3']['old_key_3']
    }

Then, for each nested dict, I could generate a new dict object with a simple for loop:

for key, value in new_dict.items() :
    user_dict_1[key] = OldDict[value]

The nested dicts are very large and I only need a few fields from each, otherwise I could just use the .copy() function to work with the old dicts.

PS- Any help in rewriting this question to be more readable also appreciated.

You're going to need reduce() for this one...

attrmap = {
  "new_key_1": ('subdict1', 'subdict2', 'old_key_1'),
   ...
}

print reduce(lambda x, y: x[y], attrmap[somekey], old_object)

Are you talking something like this?

from pprint import pprint as pp
subdict1 = {'subdict1_item1':1, 'subdict1_item2':2}
subdict2 = {'subdict2_item1':3, 'subdict2_item2':4}
subdict3 = {'subdict3_item1': 5, 'subdict3_item1':6}
olddict = {
    'old_key_1': [subdict1, subdict2],
    'old_key_2': [subdict1, subdict2],
    'old_key_3': [subdict1, subdict3],
    }

newdict = {
    'new_key_1': olddict['old_key_1'].append('old_key_1'),
    'new_key_2': olddict['old_key_2'].append('old_key_2'),
    'new_key_3': olddict['old_key_3'].append('old_key_3'),
    }

or this

newdict = {
    'new_key_1': 'old_key_1',
    'new_key_2': 'old_key_2',
    'new_key_3': 'old_key_3',
    }
def getnew(newkey, newdict, olddict):
    if newkey in newdict:
        oldkey = newdict[newkey]
        if oldkey in olddict:
            preitem = olddict[ oldkey ] # returns a list with two items
            item = []
            item.append([preitem[0]]) # makes subdict1 wrapped in a list
            item.append([preitem[1]]) # makes subdict2/3 wrapped in a list
            item.append([oldkey])
            return item
        else:
            raise KeyError('newdict has no matching olddict key')

results to:

pp( getnew('new_key_1', newdict, olddict) )
print 
pp( getnew('new_key_2', newdict, olddict) )
print
pp( getnew('new_key_3', newdict, olddict) )
[[{'subdict1_item1': 1, 'subdict1_item2': 2}],
 [{'subdict2_item1': 3, 'subdict2_item2': 4}],
 ['old_key_1']]

[[{'subdict1_item1': 1, 'subdict1_item2': 2}],
 [{'subdict2_item1': 3, 'subdict2_item2': 4}],
 ['old_key_2']]

[[{'subdict1_item1': 1, 'subdict1_item2': 2}],
 [{'subdict3_item1': 6}],
 ['old_key_3']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM