Line Dictionary to Single
This question already has an answer here:
假设你要求多个字典(不是多行字典)到一个字典。
a = {1: 1, 2:2}
b = {2:2, 3:3}
c = {2:3}
{**a, **b, **c}
Out: {1: 1, 2: 3, 3: 3}
Assuming that your initial data is actually a list of dictionaries and your keys are unique accross all your dictionaries I would use something like -
example = [{'a':1}, {'b':2}, {'c':3}]
objOut = {}
for d in example:
for k,v in d.iteritems():
objOut[k] = v
OR
objIn = [{'a':1}, {'b':2}, {'c':3}]
objOut = {}
for d in objIn:
objOut.update(d)
print objOut
Given
dicts = [{'a':1}, {'b':2}, {'c':3, 'd':4}]
Do
{k:v for d in dicts for (k, v) in d.items()}
or
from itertools import chain
dict(chain(*map(dict.items, dicts)))
resulting in
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
链接地址: http://www.djcxy.com/p/17574.html
上一篇: 发布未选中的复选框
下一篇: 线字典到单