如何使用二进制数组值迭代所有二维数组 - python?
How to make Iterate all 2d array with binary array value - python?
我想要什么:
迭代 1 :
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
迭代 2 :
[[1, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
迭代 2 :
[[1, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
迭代-1(最后一次迭代):
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]
我是这样想的:
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)
结果
[[[0, 0], [0, 0]],
[[0, 0], [0, 1]],
[[0, 0], [1, 0]],
...
[[1, 1], [1, 1]]]
我想要什么:
迭代 1 :
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
迭代 2 :
[[1, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
迭代 2 :
[[1, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
迭代-1(最后一次迭代):
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]
我是这样想的:
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)
结果
[[[0, 0], [0, 0]],
[[0, 0], [0, 1]],
[[0, 0], [1, 0]],
...
[[1, 1], [1, 1]]]