简体   繁体   中英

python dynamically create dict of dicts

first of all I wasn't able to find any suggestion to address me to solve my following question, so in case anybody already replied to it please be kind to address me to that.

I'm trying to "dynamically" create a dictionary composed by other dictionaries, (these last ones are data obtained via json); anyway the result I'm trying to achieve is the following:

{
'D2Key1': {'D1Key1': 'Data11', 'D1Key2': 'Data21', 'D1Key3': 'Data31'}, 
'D2Key2': {'D1Key1': 'Data12', 'D1Key2': 'Data22', 'D1Key3': 'Data32'},
'D2Key3': {'D1Key1': 'Data13', 'D1Key2': 'Data23', 'D1Key3': 'Data33'}
}

The code I wrote is:

for n in range(3):
    D1['D1key1'] = pjson['jsonkey1'][n]['jsonkey4'] # Data1n
    D1['D1key2'] = pjson['jsonkey2'][n]['jsonkey5'] # Data2n
    D1['D1key3'] = pjson['jsonkey3'][n]['jsonkey6'] # Data3n

    D2['%s%d' % ('D2key', n+1)] = D1

The result I get is a dictionary populated with three dictionary but all of them have the same data, the ones from last iteration, in other words something like this:

{
'D2Key1': {'D1Key1': 'Data13', 'D1Key2': 'Data23', 'D1Key3': 'Data33'}, 
'D2Key2': {'D1Key1': 'Data13', 'D1Key2': 'Data23', 'D1Key3': 'Data33'},
'D2Key3': {'D1Key1': 'Data13', 'D1Key2': 'Data23', 'D1Key3': 'Data33'}
}

Can somebody address me in the right direction? Thanks

Yes, you have to reset D1 after every pass if you do it this way, otherwise the pointer stays the same, and you point all keys in D2 to the same D1. Here's how to fix that:

for n in range(3):
    D1 = {}
    D1['D1key1'] = pjson['jsonkey1'][n]['jsonkey4'] # Data1n
    D1['D1key2'] = pjson['jsonkey2'][n]['jsonkey5'] # Data2n
    D1['D1key3'] = pjson['jsonkey3'][n]['jsonkey6'] # Data3n

    D2['%s%d' % ('D2key', n+1)] = D1

This is a common error. Every key in D2 is being assigned to the same object, D1. Then you keep modifying D1 each iteration.

You want to assign a different dictionary on each iteration, instead. Add a D1 = {} to the first line of your for loop.

This might be because you are referring to the same dict D1 inside the for loop. I think you must create new dict in each iteration. Something like this:

for n in range(3):
    D1 = {}
    D1['D1key1'] = pjson['jsonkey1'][n]['jsonkey4'] # Data1n
    D1['D1key2'] = pjson['jsonkey2'][n]['jsonkey5'] # Data2n
    D1['D1key3'] = pjson['jsonkey3'][n]['jsonkey6'] # Data3n

    D2['%s%d' % ('D2key', n+1)] = D1

This line

 D2['%s%d' % ('D2key', n+1)] = D1

is wrong. You are always pointing to the dictionary which is a mutable type. You need to copy the result first and add it to the dictionary D2.

Try this where you create a new dictionary in each loop, fill the keys and then add it to the dictionary D2.

for n in range(3):
    d1 = {}  # notice here this is what you need to add.
    d1['D1key1'] = pjson['jsonkey1'][n]['jsonkey4'] # Data1n
    d1['D1key2'] = pjson['jsonkey2'][n]['jsonkey5'] # Data2n
    d1['D1key3'] = pjson['jsonkey3'][n]['jsonkey6'] # Data3n
    d2['%s%d' % ('D2key', n+1)] = d1

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