【发布时间】:2019-07-26 13:24:26
【问题描述】:
我想在 for 循环中附加字典,以便得到一个连接字典。此外,所有字典的键不必完全相同。
等价
one={'a': '2', 'c': 't', 'b': '4'}
two={'a': '3.4', 'c': '7.6'}
three={'a': 1.2, 'c': 3.4, 'd': '2.3'}
输出:
combined={'a':['2','3.4','1.2'],'b':'4','c':['t','7.6','3.4'],
'd':'2.3'}
现在回到原来的问题:
每次 for 循环迭代时,都会生成一个字典,我想附加它。
类似:
emptydict={}
for x in z:
newdict=x.dict()
emptydict.append(newdict)
print(emptydict)
【问题讨论】:
-
伪代码:您必须遍历 newdict 的槽键并查找并附加到 combined[thiskey] ,而不是组合。
标签: python python-3.x dictionary for-loop