简体   繁体   English

遍历字典列表和列表值

[英]Loop over list of dictionaries and within list values

Suppose I have a list of two dictionaries: 假设我有两个字典的列表:

iter_list = [{0: [2, 1, 3], 
              1: [3, 2, 1], 
              2: [1, 2, 3], 
              3: [2, 3, 1], 
              4: [1, 2, 3], 
              5: [2, 3, 1]},
             {0: [2, 3, 1], 
              1: [1, 2, 3], 
              2: [1, 2, 3], 
              3: [1, 2, 3], 
              4: [2, 3, 1], 
              5: [1, 3, 2]}]

Each dictionary has 6 keys numbered 0 through 5. I would like to loop through each dictionary one at a time in the order of the key and have the output be a pair with the key (ordered by the key value) and the first value of the corresponding list, the first value corresponding with the second key etc. followed by the key and the second value etc. Hopefully the example output will clarify: 每个词典有6个键,编号为0到5。我想一次按键顺序遍历每个词典,并让输出与键成对(按键值排序)和的第一个值。相应的列表,第一个值与第二个键等对应,然后是键和第二个值等。希望示例输出可以阐明:

0 2
1 3
2 1
3 2
4 1
5 2
0 1
1 2
2 2
3 3
4 2
5 3
0 3
1 1
2 3
3 1
4 3
5 1
0 2 //2nd dictionary iteration 
1 1
2 1
3 1
4 2
5 1
0 3 
1 2
2 2
3 2
4 3
5 3
0 1
1 3
2 3
3 3
4 1
5 2

I've only been able to figure out how to loop through the first position of the first dictionary, but can't figure out how to use the [0] as an iteration variable to go through all positions in the value lists before moving on to the next dictionary. 我只能弄清楚如何在第一个字典的第一个位置循环,但不能弄清楚在继续之前如何使用[0]作为迭代变量遍历值列表中的所有位置转到下一个字典。

for i in iter_list:
    for key, value in i.iteritems():
            print key, value[0]

Any help is greatly appreciated! 任何帮助是极大的赞赏! Thanks! 谢谢!

If I understand you correctly, in your case, you need to iterate the iter_list 3 times 如果我正确理解您的情况,则需要迭代iter_list 3次

>>> for i in iter_list:
...     for x in range(3):
...         for key, value in i.iteritems():
...             print key, value[x]
...

Output: 输出:

0 2
1 3
2 1
3 2
4 1
5 2
0 1
1 2
2 2
3 3
4 2
5 3
0 3
1 1
2 3
3 1
4 3
5 1
0 2
1 1
2 1
3 1
4 2
5 1
0 3
1 2
2 2
3 2
4 3
5 3
0 1
1 3
2 3
3 3
4 1
5 2

A different approach using zip and itertools.chain and unpacking tricks: 使用zipitertools.chain 技巧的另一种方法:

from itertools import chain
packed_pairs = zip(*[[(k, v) for v in vs] for ds in iter_list for k, vs in ds.items()])
pairs = chain(*packed_pairs)
for pair in pairs:
    print pair[0], pair[1]

Output: 输出:

0 2
1 3
2 1
3 2
4 1
5 2
0 2
1 1
2 1
3 1
4 2
5 1
0 1
1 2
2 2
3 3
4 2
5 3
0 3
1 2
2 2
3 2
4 3
5 3
0 3
1 1
2 3
3 1
4 3
5 1
0 1
1 3
2 3
3 3
4 1
5 2

Note: make sure to use pairs = list(pairs) if you want to re-use pairs . 注意:一定要使用pairs = list(pairs) ,如果你想重新使用pairs

A list comprehension should do: 列表理解应该做到:

>>> output_list = [[k, d[k][i]] for d in iter_list for i in range(3) for k in sorted(d)]: 
>>> for k, dki in output_list:
...     print k, dki
...
0 2
1 3
2 1
3 2
4 1
5 2
0 1
1 2
2 2
3 3
4 2
5 3
0 3
1 1
2 3
3 1
4 3
5 1
0 2
1 1
2 1
3 1
4 2
5 1
0 3
1 2
2 2
3 2
4 3
5 3
0 1
1 3
2 3
3 3
4 1
5 2

perhaps you want to do this: 也许您想这样做:

for i in iter_list:
    for j in range(3):
        for key, value in i.iteritems():
            print key, value[j]

assuming the values in the dictionaries have a fixed length of 3, otherwise you have to figure out where 3 comes from. 假设字典中的值的固定长度为3,否则您必须弄清楚3的来源。 For example, it could be the minimum value length across all values of all dictionaries: 例如,它可以是所有字典的所有值的最小值长度:

>>> min(len(v) for k,v in i.iteritems() for i in iter_list)
3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM