2016-03-03 115 views
-4

我的字典[{'abc':10,'efg':20,'def':30},{'abc':40,'xya':20,'def':50}]的名单,我想在创建一个数组abc[]并存储相应的值array.so输出应该看起来像迭代数组

abc[10,40] 
def[30,50] 
efg[20] 

等字典和储值上,使用python。

+0

所以你想得到的数组的名称是字典的关键。正确? – zaxliu

+5

到目前为止,您尝试过哪些方法?尝试实施解决方案时遇到了哪些问题? –

+0

欢迎来到StackOverflow。请阅读并遵守帮助文档中的发布准则。 [最小,完整,可验证的示例](http://stackoverflow.com/help/mcve)适用于此处。在您发布代码并准确描述问题之前,我们无法有效帮助您。 StackOverflow不是一个编码或教程服务。 – Prune

回答

0

任何确切的解决方案可能会涉及到的exec()或东西的陌生人,最Python程序员可能会鼓励你,而不是改变你的词典列表插入列表的词典:

from collections import defaultdict 

list_of_dictionaries = [ 
    {'abc':10,'efg':20,'def':30}, 
    {'abc':40,'xya':20,'def':50}, 
] 

dictionary_of_lists = defaultdict(list) 

# there's probably some clever one liner to do this but let's keep 
# it simple and clear what's going when we make the transfer: 

for dictionary in list_of_dictionaries: 
    for key, value in dictionary.items(): 
     dictionary_of_lists[key].append(value) 

# We've achieved the goal, now just dump dictionary_of_lists to prove it: 

for key, value in dictionary_of_lists.items(): 
    print(key, value) 

,输出:

xya [20] 
def [30, 50] 
abc [10, 40] 
efg [20] 

不完全是你要求的,但应该是,为了大多数目的,你需要什么。