如何使用带有任意数量键的for循环和zip访问嵌套的dict元素

时间:2019-08-29 14:24:34

标签: python list dictionary

我有一个嵌套的字典,在最底层有一个结果变量列表。到达这些列表并同时对其执行操作的最有效方法是什么?

我目前在zip()中使用嵌套的for循环来访问底层列表并同时执行操作。当顶层具有大量键(> 5)时,我的方法不可行。

participants = ['p1', 'p2']
conditions = ['a','b','c','d']
outcomes = ['x','y','z']

result_dict = {i : {j : {k : [] for k in outcomes} for j in conditions} for i in participants}      

#current method, which is fine for small numbers of participants
dat = zip(*result_dict.items())
for p1,p2 in zip(dat[1][0].items(), dat[1][1].items()):
    for a,b in zip(p1[1].items(),p2[1].items()):

        #perform operations
        print a[1] + b[1]

0 个答案:

没有答案