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.