我想要的是 :
迭代 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]]
uj5u.com熱心網友回復:
我是這樣想的:
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]]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/480952.html
標籤:Python 数组 python-3.x 循环
