简体   繁体   中英

How to Change a Dictionaries Key Based on the Value Designated to the Same Key in Another Dictionary?

I am trying to update a dictionary key using key value pairs in another dictionary. The two dictionaries I am trying to combine are both nested into lists:

dictionary1 = [{ '32639': {'78549': {'526' : { 'sum': 8930.40, 'min' : 2380, 'max': 74839}}}} , {'89304': {'20384': {'152' : { 'sum': 51235.20, 'min' : 4512, 'max': 362.69}}}}, { '41526': {'45315': {'364' : { 'sum': 8985.65, 'min' : 3632.32, 'max': 4558.15}}}}]

dictionary2 = [{'32639':'90283'}, {'49034': '89203'}, {'28942': '39024'}, {'41526':'24903'} ]

I want the resulting dictionary to look exactly like dictionary1 however if the key of the dictionaries in dictionary1 is in the keys in the dictionaries of dictionary2 they should be changed.

Resulting dictionary:

new_dictionary = [{ '90283': {'78549': {'526' : { 'sum': 8930.40, 'min' : 2380, 'max': 74839}}}} , {'89304': {'20384': {'152' : { 'sum': 51235.20, 'min' : 4512, 'max': 362.69}}}}, { '24903': {'45315': {'364' : { 'sum': 8985.65, 'min' : 3632.32, 'max': 4558.15}}}}]

I have attempted:

list1 = []
for d1, d2 in zip(dictionary1, dictionary2):
    for key, value in d1.iteritems():
        new_dict = {}
        if key in d2:
            new_dict[d2[key]] = value
            list1.append(new_dict)
        else:
            new_dict[key] = value
            list1.append(new_dict)

However it is not working, on this sample data it works however, the program only iterates through dictionary1 based on the length of dictionary2. So I am trying to run this with a list that has 841 dictionaries (dictionary 1) and a list of dictionaries of 53 (dictionary 2) and it will only convert the first 53 keys in dictionary 1 before it quits.

Try something like this:

ds1 = [{ '32639': {'78549': {'526' : { 'sum': 8930.40, 'min' : 2380, 'max': 74839}}}} , {'89304': {'20384': {'152' : { 'sum': 51235.20, 'min' : 4512, 'max': 362.69}}}}, { '41526': {'45315': {'364' : { 'sum': 8985.65, 'min' : 3632.32, 'max': 4558.15}}}}]

ds2 = [{'32639':'90283'}, {'49034': '89203'}, {'28942': '39024'}, {'41526':'24903'} ]

def trans(ds1, ds2):
    for d1 in ds1:
        for d2 in ds2:
            ckeys = d1.keys() & d2.keys()
            ukeys = d1.keys() - d2.keys()
            for ck in ckeys:
                yield (d2[ck], d1[ck])
            for uk in ukeys:
                yield (uk, d1[uk])
print(dict(trans(ds1, ds2)))

I got output:

{'90283': {'78549': {'526': {'sum': 8930.4, 'min': 2380, 'max': 74839}}}, '32639': {'78549': {'526': {'sum': 8930.4, 'min': 2380, 'max': 74839}}}, '89304': {'20384': {'152': {'sum': 51235.2, 'min': 4512, 'max': 362.69}}}, '41526': {'45315': {'364': {'sum': 8985.65, 'min': 3632.32, 'max': 4558.15}}}, '24903': {'45315': {'364': {'sum': 8985.65, 'min': 3632.32, 'max': 4558.15}}}}

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