Wenn ich verstehe, was Sie suchen, benutzen Sie itertools.product() über die powersets der Eingänge verwenden können (es ist ein Rezept für powerset() in der Dokumentation). Die map() Funktion kann verwendet werden, um powerset auf jeden der Eingänge anzuwenden.
from itertools import product, combinations, chain
from pprint import pprint
def powerset(iterable):
"powerset([1,2,3]) -->() (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
names = ['a', 'b', 'c']
a = ["x", "y"]
b = ["q", "w", "c"]
c = ["i", "o", "p"]
result = []
for values in product(*map(powerset, [a, b, c])):
result.append(dict(zip(names, values)))
pprint(result)
Hier ist, wie es funktioniert:
Zuerst baut er die Kräftesets:
>>> list(powerset(["x", "y"]))
[(), ('x',), ('y',), ('x', 'y')]
>>>
>>> list(powerset(["x", "y"]))
[(), ('x',), ('y',), ('x', 'y')]
>>> list(powerset(["q", "w", "c"]))
[(), ('q',), ('w',), ('c',), ('q', 'w'), ('q', 'c'),
('w', 'c'), ('q', 'w', 'c')]
>>> list(powerset(["i", "o", "p"]))
[(), ('i',), ('o',), ('p',), ('i', 'o'), ('i', 'p'),
('o', 'p'), ('i', 'o', 'p')]
Weiter product() zieht ein Element aus jeder Powerset:
>>> for values in product(*map(powerset, [a, b, c])):
print(values)
((),(),())
((),(), ('i',))
((),(), ('o',))
((),(), ('p',))
((),(), ('i', 'o'))
((),(), ('i', 'p'))
((),(), ('o', 'p'))
((),(), ('i', 'o', 'p'))
((), ('q',),())
((), ('q',), ('i',))
((), ('q',), ('o',))
((), ('q',), ('p',))
((), ('q',), ('i', 'o'))
((), ('q',), ('i', 'p'))
((), ('q',), ('o', 'p'))
((), ('q',), ('i', 'o', 'p'))
Schließlich , wir zip() zusammen er die obigen Ergebnisse mit den variablen Namen machen ein dict():
# What zip does
>>> list(zip(['a', 'b', 'c'], ((), ('q',), ('i', 'o', 'p'))))
[('a',()), ('b', ('q',)), ('c', ('i', 'o', 'p'))]
# What dict does with the zip:
>>> dict(zip(['a', 'b', 'c'], ((), ('q',), ('i', 'o', 'p'))))
{'b': ('q',), 'c': ('i', 'o', 'p'), 'a':()}
Ihr Ausgang ist nicht so klar. Bitte geben Sie weitere Beispiele an. –