Let's say I wanted to get all the possible combinations of three binary digits, i.e:
0,0,0
0,0,1
0,1,0
0,1,1
1,0,0
1,0,1
1,1,0
1,1,1
I could do something like this:
p = []
for a in range(2):
for b in range(2):
for c in range(2):
p.append([a,b,c])
print p
But what if I wanted to define a function that returns the possiblities for n numbers of binary digits? i.e. How can I stack the for loops dynamically?
from itertools import product
product(range(2), repeat=3)
Take a look at itertools.product.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With