简体   繁体   English

设定值排列字典的Python递归

[英]Python recursion over dictionary of set values permutations

I'm trying to get all combinations of a dictionary recursively but I can't wrap my head around how to get the output properly. 我试图递归地获取字典的所有组合,但是我无法解决如何正确获取输出的问题。 Essentially a depth-first search which saves the input in a (key, value) tuple or something similar. 本质上是深度优先搜索,它将输入保存在(键,值)元组或类似内容中。 Any help appreciated. 任何帮助表示赞赏。 Thanks /Fred 谢谢/弗雷德

input: 输入:

d = {"item1": {1, 2},
     "item2": {3, 4},
     "item3": {5, 6}}

output: 输出:

"item1" 1
"item2" 3
"item3" 5
"item3" 6
"item2" 4
"item3" 5
"item3" 6
"item1" 2
"item2" 3
"item3" 5
"item3" 6
"item2" 4
"item3" 5
"item3" 6

edit: The perms needs to be drawn recursively. 编辑:烫发需要递归绘制。 Maybe an illustration clarifies a bit: 也许插图可以澄清一下: 在此处输入图片说明

A tree structure worked but was not generalised enough for my purposes, and cumbersome to edit. 树形结构可以工作,但对于我的目的而言,归纳不够充分,并且难以编辑。

Update: Currently I'm hardcoding these like so: 更新:目前,我正在对这些进行硬编码,如下所示:

d = {"item1": {1, 2},
     "item2": {3, 4},
     "item3": {i for i in range(1, 5)}}

for k in d["item1"]:
    print ("item1", k)
    for j in d["item2"]:
        print ("item2", j)
        for i in d["item3"]:
            print("item3", i)

It seems obvious where the recursion happens but I'm still having troubles with it. 递归发生的地方似乎很明显,但是我仍然遇到麻烦。 Thank you all for all suggestions so far! 谢谢大家到目前为止的所有建议! Also it's in python3 if that makes any difference. 如果有什么不同,它也位于python3中。

This will return a list of permutations. 这将返回排列列表。

from itertools import product

perms = [[perm for perm in product([key], d[key]) for key in d]]

An update in case you're looking for a possible combinations of key values pairs which would be 18 in total. 如果您正在寻找可能的键值对组合(总共为18)的更新。

[print(prod) for prod in product(d, itertools.chain.from_iterable(d.values()))]

outputs: 输出:

('item3', 5)
('item3', 6)
('item3', 1)
('item3', 2)
('item3', 3)
('item3', 4)
('item1', 5)
('item1', 6)
('item1', 1)
('item1', 2)
('item1', 3)
('item1', 4)
('item2', 5)
('item2', 6)
('item2', 1)
('item2', 2)
('item2', 3)
('item2', 4)

How about this: 这个怎么样:

>>> for pair in itertools.chain(*([(k, i) for i in v] for k, v in d.items())):
    print(pair)

('item2', 3)
('item2', 4)
('item3', 5)
('item3', 6)
('item1', 1)
('item1', 2)

Or a bit more legible: 或更清晰:

def key_value_permutations(d):
    for k, v in d:
       for p in itertools.product([k], v):
           yield p

Then: 然后:

>>> for p in key_value_permutations(d):
    print p

('item2', 3)
('item2', 4)
('item3', 5)
('item3', 6)
('item1', 1)
('item1', 2)

Both solutions use generators because they assume your dicts and sets will be bigger than in the sample you provided and you will just iterate over it so you don't really need to create a huge list in memory. 两种解决方案都使用生成器,因为它们假定您的dict和set大于您提供的示例中的dict和set,并且您将对其进行迭代,因此您实际上不需要在内存中创建庞大的列表。 If the size is negligible, list comprehensions may be faster. 如果大小可以忽略不计,则列表理解可能会更快。

You can't count on consistent ordering because both dicts and sets have no guaranteed ordering. 您不能指望一致的排序,因为字典和集合都没有保证的排序。

Is this what you're looking for? 这是您要找的东西吗?

for first_level in d:
     item_list = d[first_level]
     for item in item_list:
             print((first_level, item))

Output 输出量

('item2', 3)
('item2', 4)
('item3', 5)
('item3', 6)
('item1', 1)
('item1', 2)

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

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