[英]How do I use a nested for loop to write from one list to another in Python?
I have two lists:我有两个清单:
import random
board = [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
]
boxes = [random.sample(range(1, 10), 9), random.sample(range(1, 10), 9), random.sample(range(1, 10), 9)]
The first one is a sudoku board and the second one is three lists of 9 random non-repeating numbers.第一个是数独板,第二个是 9 个随机不重复数字的三个列表。 And I want to insert the random numbers into the sudoku board so the result board looks something like this:
我想将随机数插入数独板,所以结果板看起来像这样:
[5, 4, 3, 0, 0, 0, 0, 0, 0],
[2, 9, 1, 0, 0, 0, 0, 0, 0],
[6, 7, 8, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 6, 3, 0, 0, 0],
[0, 0, 0, 4, 5, 8, 0, 0, 0],
[0, 0, 0, 9, 2, 7, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 3, 8, 2],
[0, 0, 0, 0, 0, 0, 5, 9, 6],
[0, 0, 0, 0, 0, 0, 7, 1, 4]
I can achieve what I want with this code:我可以用这段代码实现我想要的:
# BOX 1
board[0][0] = boxes[0][0]
board[0][1] = boxes[0][1]
board[0][2] = boxes[0][2]
board[1][0] = boxes[0][3]
board[1][1] = boxes[0][4]
board[1][2] = boxes[0][5]
board[2][0] = boxes[0][6]
board[2][1] = boxes[0][7]
board[2][2] = boxes[0][8]
# BOX 2
board[3][3] = boxes[1][0]
board[3][4] = boxes[1][1]
board[3][5] = boxes[1][2]
board[4][3] = boxes[1][3]
board[4][4] = boxes[1][4]
board[4][5] = boxes[1][5]
board[5][3] = boxes[1][6]
board[5][4] = boxes[1][7]
board[5][5] = boxes[1][8]
# BOX 3
board[6][6] = boxes[2][0]
board[6][7] = boxes[2][1]
board[6][8] = boxes[2][2]
board[7][6] = boxes[2][3]
board[7][7] = boxes[2][4]
board[7][8] = boxes[2][5]
board[8][6] = boxes[2][6]
board[8][7] = boxes[2][7]
board[8][8] = boxes[2][8]
But obviously that is a super long and dumb way to do this but for the love of god I cannot figure out how to do this using a nested for loop, but it seems like it should be possible?但显然这是一种超级漫长而愚蠢的方法,但看在上帝的份上,我无法弄清楚如何使用嵌套的 for 循环来做到这一点,但它似乎应该是可能的?
I thought I could atleast fill the first box with this code:我想我至少可以用这段代码填充第一个框:
for x in range(9):
for y in range(3):
board[y][x] = boxes[0][x]
But even that does not work as intended...please help...但即使这样也不能按预期工作......请帮助......
This will do it.这会做到的。 There are better ways if you are willing to use numpy.
如果您愿意使用 numpy,还有更好的方法。
for box in range(3):
# Get starting location in board.
dx = box*3
for k in range(9):
x = k % 3
y = k // 3
board[dx+y][dx+x] = boxes[box][k]
Alternatively:或者:
for box in range(3):
# Get starting location in board.
dx = box*3
for y in range(3):
for k in range(3):
board[dx+y][dx+x] = boxes[box][y*3+x]
You can directly generate board like this:您可以像这样直接生成板:
import random
board = [random.sample(range(1, 10), 3)+[0]*6, random.sample(range(1, 10), 3)+[0]*6, random.sample(range(1, 10), 3)+[0]*6,
[0]*3+random.sample(range(1, 10), 3)+[0]*3, [0]*3+random.sample(range(1, 10), 3)+[0]*3, [0]*3+random.sample(range(1, 10), 3)+[0]*3, [0]*6+random.sample(range(1, 10), 3), [0]*6+random.sample(range(1, 10), 3), [0]*6+random.sample(range(1, 10), 3)]
print(board)
Output Output
[
[9, 6, 7, 0, 0, 0, 0, 0, 0],
[2, 6, 3, 0, 0, 0, 0, 0, 0],
[8, 7, 4, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 7, 3, 8, 0, 0, 0],
[0, 0, 0, 5, 1, 6, 0, 0, 0],
[0, 0, 0, 6, 8, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 2, 9, 7],
[0, 0, 0, 0, 0, 0, 3, 4, 8],
[0, 0, 0, 0, 0, 0, 9, 5, 6]
]
A more neater version using functions:使用函数的更简洁的版本:
import random
def f1():
return random.sample(range(1, 10), 3)+[0]*6
def f2():
return [0]*3+random.sample(range(1, 10), 3)+[0]*3
def f3():
return [0]*6+random.sample(range(1, 10), 3)
board = [f1(),f1(),f1(),f2(),f2(),f2(),f3(),f3(),f3()]
print(board)
import numpy as np
def rand3x3():
return np.random.randint(1,9,(3,3))
board = np.zeros((9,9), dtype=int)
for square3x3 in [[0,3],[3,6],[6,9]]:
start = square3x3[0]
end = square3x3[1]
board[start:end, start:end] += rand3x3()
board.tolist()
Output Output
[[2, 6, 4, 0, 0, 0, 0, 0, 0],
[4, 3, 3, 0, 0, 0, 0, 0, 0],
[8, 4, 6, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 4, 6, 1, 0, 0, 0],
[0, 0, 0, 6, 7, 3, 0, 0, 0],
[0, 0, 0, 7, 2, 6, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 8, 2],
[0, 0, 0, 0, 0, 0, 3, 2, 6],
[0, 0, 0, 0, 0, 0, 7, 4, 1]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.