你可以这样做:
al = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}, {'a': 7, 'b': 8, 'c': 9}, {'a': 10, 'b': 11, 'c': 12}]
result={}
for dic in al:
for key,val in dic.items():
result.setdefault(key,[]).append(val)
print result
# {'a': [1, 4, 7, 10], 'c': [3, 6, 9, 12], 'b': [2, 5, 8, 11]}
a,b,c=result['a'],result['b'],result['c']
print a,b,c
# [1, 4, 7, 10] [2, 5, 8, 11] [3, 6, 9, 12]
如果要将字典中的名称绑定到当前名称空间中的名称,可以这样做:
al = [{'a': 1, 'b': 2, 'c': 3, 'd':4},
{'a': 4, 'b': 5, 'c': 6},
{'a': 7, 'b': 8, 'c': 9}]
result={}
for dic in al:
for k in dic:
result.setdefault(k,[]).append(dic[k])
print result
# {'a': [1, 4, 7, 10], 'c': [3, 6, 9, 12], 'b': [2, 5, 8, 11], 'd': [4]}
for k in result:
code=compile('{}=result["{}"]'.format(k,k), '<string>', 'exec')
exec code
print a,b,c,d
# [1, 4, 7, 10] [2, 5, 8, 11] [3, 6, 9, 12] [4]