Home > Net >  How to make Iterate all 2d array with binary array value - python?
How to make Iterate all 2d array with binary array value - python?

Time:05-24

what i want :

iterate 1 :

[[0, 0, 0, 0],
 [0, 0, 0, 0],
 [0, 0, 0, 0],
 [0, 0, 0, 0]]

iterate 2 :

[[1, 0, 0, 0],
 [0, 0, 0, 0],
 [0, 0, 0, 0],
 [0, 0, 0, 0]]

iterate 2 :

[[1, 1, 0, 0],
 [0, 0, 0, 0],
 [0, 0, 0, 0],
 [0, 0, 0, 0]]

iterate -1(Last iterate):

[[1, 1, 1, 1],
 [1, 1, 1, 1],
 [1, 1, 1, 1],
 [1, 1, 1, 1]]

CodePudding user response:

i think like this :

import itertools
n = 2

list_of_solution = []
for i in itertools.product([0, 1], repeat=n*n):
    sol= [list(i)[x:x n] for x in range(0, len(list(i)), n)]
    list_of_solution.append(sol)

print(list_of_solution)

Result

[[[0, 0], [0, 0]], 
[[0, 0], [0, 1]], 
[[0, 0], [1, 0]], 
...
[[1, 1], [1, 1]]]
  • Related