Reputation: 31
I have a list with the following elements: A,B,C,D,E,F,G
.
They are either suppose to true or false hence represented by 1
and 0
respectively.
I am supposed to get a combinations but the following restrictions stay:
C
and Fare to be true in all cases, ie,
1`.A
is true, element E
, and G
can be false.B
is true, element D
can be false.Upvotes: 2
Views: 345
Reputation: 40773
What you want is not permutations, but product. Also, I interpret restrictions as:
With that, the code is as followed:
import pprint
from itertools import product
def myproduct():
keys = 'abcdefg'
values = [(0, 1) for k in keys]
for value in product(*values):
d = dict(zip(keys, value))
# Skip: C and F that are 0 (False)
if d['c'] == 0 or d['f'] == 0:
continue
# Skip: When A is false, E and G cannot be false
if d['a'] == 0 and (d['e'] == 0 or d['g'] == 0):
continue
# Skip: When B is false, D cannot be false
if d['b'] == 0 and d['d'] == 0:
continue
yield d # This 'permutation' is good
for d in myproduct():
pprint.pprint(d)
Output:
{'a': 0, 'b': 0, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1}
{'a': 0, 'b': 1, 'c': 1, 'd': 0, 'e': 1, 'f': 1, 'g': 1}
{'a': 0, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1}
{'a': 1, 'b': 0, 'c': 1, 'd': 1, 'e': 0, 'f': 1, 'g': 0}
{'a': 1, 'b': 0, 'c': 1, 'd': 1, 'e': 0, 'f': 1, 'g': 1}
{'a': 1, 'b': 0, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 0}
{'a': 1, 'b': 0, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1}
{'a': 1, 'b': 1, 'c': 1, 'd': 0, 'e': 0, 'f': 1, 'g': 0}
{'a': 1, 'b': 1, 'c': 1, 'd': 0, 'e': 0, 'f': 1, 'g': 1}
{'a': 1, 'b': 1, 'c': 1, 'd': 0, 'e': 1, 'f': 1, 'g': 0}
{'a': 1, 'b': 1, 'c': 1, 'd': 0, 'e': 1, 'f': 1, 'g': 1}
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 0, 'f': 1, 'g': 0}
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 0, 'f': 1, 'g': 1}
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 0}
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1}
Notes:
values
is a list of (0, 1)
:
[(0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1)]
Each value
is a tuple of 7 numbers such as:
(1, 1, 1, 0, 0, 1, 0)
d
is a dictionary in which the keys are a, b, ... and the values are 0 and 1
Upvotes: 1