I want to get all possible enumeration results, which have 0 or 1 as value in each index.
Below is my code and the result.
How can I improve my code? I want to avoid too many for
loops.
k=[0,0,0,0,0,0]
list_dic={}
for a in range(2):
k[0]=a
for b in range(2):
k[1]=b
for c in range(2):
k[2]=c
for d in range(2):
k[3]=d
for e in range(2):
k[4]=e
for f in range(2):
k[5]=f
list_dic[tuple(k)]=0
enum_list=list((list_dic.keys()))
print(enum_list)
Maybe you want to try with intertools:
import itertools
k = [0,1]
ret = itertools.product(k, repeat = 6)
for i in ret:
print i
output:
(0, 0, 0, 0, 0, 0)
(0, 0, 0, 0, 0, 1)
(0, 0, 0, 0, 1, 0)
(...many arrays..)
(1, 1, 1, 1, 0, 1)
(1, 1, 1, 1, 1, 0)
(1, 1, 1, 1, 1, 1)
with repeat = 6 you set the large of the array out get in combination array store in k, in k you can put any value and intertools try to order and combine then