简体   繁体   English

如何遍历列表字典并将每个迭代与所有键中的项目配对?

[英]How do I iterate through a dictionary of lists and pair each iteration with an item from all the keys?

I need to iterate through a dictionary of lists, not knowing how many lists the dictionary will have, but still pair off each list value with any other list value produced from another key in the dictionary (if another one exists). 我需要遍历列表字典,不知道字典将有多少列表,但仍然将每个列表值与字典中另一个键生成的任何其他列表值配对(如果存在另一个键)。 I have the following code: 我有以下代码:

def loop_rec(codes, currentcode={}):
    if len(codes.keys())>1:
        for key in sorted(codes):
            codespop = dict(codes)
            loop = codespop.pop(key)
            for x in loop:
                currentcode[key]=x
                loop_rec(codespop,currentcode)
            break
    else:
        for key in codes.keys():
            loop = codes[key]
            for x in loop:
                currentcode[key]=x
                print currentcode

So if I have the following dictionary: 所以,如果我有以下字典:

codes = {"coarse":range(4),"fine":range(2)}

I get this result: 我得到这个结果:

>>> loop_rec(codes)
{'fine': 0, 'coarse': 0}
{'fine': 1, 'coarse': 0}
{'fine': 0, 'coarse': 1}
{'fine': 1, 'coarse': 1}
{'fine': 0, 'coarse': 2}
{'fine': 1, 'coarse': 2}
{'fine': 0, 'coarse': 3}
{'fine': 1, 'coarse': 3}

This is a brute force method and would like a more "Pythonic" way of doing this. 这是一种蛮力方法,并希望采用更“Pythonic”的方式。 I searched around a lot for something equivalent, but most methods don't result in the coarse and fine values being together for each iteration. 我搜索了大量相当的东西,但是大多数方法都没有导致粗略和精细值一起用于每次迭代。 Also would prefer it loop through coarse first, but the sorted command isn't working. 也希望它首先循环粗略,但排序的命令不起作用。

EDIT: Just realized the sorted command is working, the printout just isn't sorted. 编辑:刚刚意识到排序的命令正在工作,打印输出只是没有排序。 I don't care if it is printed in order. 我不在乎是否按顺序打印。

If I understand your question correctly, you want to take the cartesian product of all of the lists that are the values of a dict. 如果我正确理解你的问题,你想要把所有列表中的笛卡尔积作为dict的值。 You can use itertools.product to accomplish this. 您可以使用itertools.product来完成此任务。

import itertools
def dict_product(d):
    list_of_dicts = []
    for values in itertools.product(*d.values()):
        item = dict(zip(d.keys(),values))
        list_of_dicts.append(item)
    return list_of_dicts


codes = {"coarse":range(4),"fine":range(2),"zesty":range(3)}
for item in dict_product(codes):
    print(item)

Result: 结果:

{'zesty': 0, 'fine': 0, 'coarse': 0}
{'zesty': 0, 'fine': 0, 'coarse': 1}
{'zesty': 0, 'fine': 0, 'coarse': 2}
{'zesty': 0, 'fine': 0, 'coarse': 3}
{'zesty': 0, 'fine': 1, 'coarse': 0}
{'zesty': 0, 'fine': 1, 'coarse': 1}
{'zesty': 0, 'fine': 1, 'coarse': 2}
{'zesty': 0, 'fine': 1, 'coarse': 3}
{'zesty': 1, 'fine': 0, 'coarse': 0}
{'zesty': 1, 'fine': 0, 'coarse': 1}
{'zesty': 1, 'fine': 0, 'coarse': 2}
{'zesty': 1, 'fine': 0, 'coarse': 3}
{'zesty': 1, 'fine': 1, 'coarse': 0}
{'zesty': 1, 'fine': 1, 'coarse': 1}
{'zesty': 1, 'fine': 1, 'coarse': 2}
{'zesty': 1, 'fine': 1, 'coarse': 3}
{'zesty': 2, 'fine': 0, 'coarse': 0}
{'zesty': 2, 'fine': 0, 'coarse': 1}
{'zesty': 2, 'fine': 0, 'coarse': 2}
{'zesty': 2, 'fine': 0, 'coarse': 3}
{'zesty': 2, 'fine': 1, 'coarse': 0}
{'zesty': 2, 'fine': 1, 'coarse': 1}
{'zesty': 2, 'fine': 1, 'coarse': 2}
{'zesty': 2, 'fine': 1, 'coarse': 3}

In this example, the iteration order is coarse-fine-zesty, but this behavior is not guaranteed. 在此示例中,迭代顺序是粗略精细的,但不保证此行为。 In CPython 3.6 and higher, dictionaries are ordered, but this is an implementation detail and may change in the future. 在CPython 3.6及更高版本中,字典是有序的,但这是一个实现细节,将来可能会发生变化。

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

相关问题 如何通过函数将多个键与字典中的一个值配对? - How do I pair up multiple keys with one values from a dictionary through a function? 并行遍历字典中所有键值的列表? - Iterate through lists in values of all keys in a dictionary in parallel? 如何遍历字符串列表并打印每个项目? - How do I iterate through a list of strings and print each item? 如何使用循环遍历包含列表的字典、计算某些内容并将结果添加到字典中? - How do I use a loop to iterate through a dictionary that contains lists, calculate something, and add the result to the dictionary? 如何从列表列表中创建字典键? - How do I create dictionary keys from list of lists? 我如何遍历列表 - how do I iterate through a list of lists 如何迭代字典中的每对项目 - How to iterate over each pair of items in a dictionary 如何从字典中创建每个键的排列? - How do I create permutations of of each keys from dictionary? 如何从字典的每个值列表中选择一项来获得所有排列? - How do I get all the permutations selecting one item from each value list in a dictionary? 多次遍历三个列表,在每次迭代中从列表中依次选择一个元素 - Iterate through three lists multiple times sequentially picking one element from a list in each iteration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM