Find all posibililties in a dict (Python 2.7)

I have the following dict in python:

{main1: {x: 1, y: 2}, main2: {a: 1, b: 2}}

As a result, I need all possible combinations, for example:

{main1: {x: 1}}
{main1: {y: 2}}
{main1: {x: 1, y:2}}
{main2: {a: 1}}
{main1: {x: 1}, main2: {a: 1}}
{main1: {y: 2}, main2: {a: 1}}
{main1: {x: 1, y:2}, main2: {a: 1}}
...

etc .. I feel that there is some kind of pythonic solution, but I can not find it. Any ideas?

+4
source share
2 answers
itertools.combinations()

can help you. You can call it by getting combinations of sizes 1, 2, and so on in the base loop.

+3
source

Try the following:

import itertools
[zip(x,main2) for x in itertools.combinations(main1,len(main2))]
+1
source

Source: https://habr.com/ru/post/1598093/


All Articles