简体   繁体   English

python动态创建dicts的词典

[英]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); 我正在尝试“动态”创建由其他词典组成的字典(这些最后一个是通过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. 是的,如果你这样做,你必须在每次传递后重置D1,否则指针保持不变,并将D2中的所有键指向相同的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. D2中的每个键都被分配给同一个对象D1。 Then you keep modifying D1 each iteration. 然后你每次迭代都要继续修改D1。

You want to assign a different dictionary on each iteration, instead. 您希望在每次迭代时分配不同的字典。 Add a D1 = {} to the first line of your for loop. 将一个D1 = {}添加到for循环的第一行。

This might be because you are referring to the same dict D1 inside the for loop. 这可能是因为你在for循环中引用了相同的dict D1。 I think you must create new dict in each iteration. 我认为你必须在每次迭代中创建新的dict。 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. 您需要先复制结果并将其添加到字典D2中。

Try this where you create a new dictionary in each loop, fill the keys and then add it to the dictionary D2. 尝试在每个循环中创建新词典,填充键然后将其添加到词典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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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