如何在python中创建3x3数独块列表 [英] How to create lists of 3x3 sudoku block in python
问题描述
我需要帮助为数独中的9个3x3块中的每个块创建一个列表.所以我有一个代表原始数独板的列表列表(零表示空):
I need help creating a list for each of the 9 3x3 blocks in sudoku. so I have a list of lists representing the original sudoku board (zero means empty):
board=[[2,0,0,0,0,0,0,6,0],
[0,0,0,0,7,5,0,3,0],
[0,4,8,0,9,0,1,0,0],
[0,0,0,3,0,0,0,0,0],
[3,0,0,0,1,0,0,0,9],
[0,0,0,0,0,8,0,0,0],
[0,0,1,0,2,0,5,7,0],
[0,8,0,7,3,0,0,0,0],
[0,9,0,0,0,0,0,0,4]]
我需要将它们变成包含3x3块的列表列表.例如:
I need to turn these into a list of lists containing the 3x3 blocks. So for example:
[[2,0,0,0,0,0,0,4,8],[etc]]
我尝试创建一个名为块"的列表,其中包含9个其他列表,每个列表中只有零个.看起来像这样:
i tried creating one list called "blocks" containing 9 other lists with just zeroes in each list. so it looked like:
blocks=[[0,0,0,0,0,0,0,0,0],[etc]
然后我使用了while循环来更改列表中的值:
then i used a while loop to change the values in the list:
BLOCK_COUNT=0
BOARD_COUNT=0
while BLOCK_COUNT<len(blocks):
blocks[BLOCK_COUNT][0]=board[BOARD_COUNT][BOARD_COUNT]
blocks[BLOCK_COUNT][1]=board[BOARD_COUNT][BOARD_COUNT+1]
blocks[BLOCK_COUNT][2]=board[BOARD_COUNT][BOARD_COUNT+2]
blocks[BLOCK_COUNT][3]=board[BOARD_COUNT+1][BOARD_COUNT]
blocks[BLOCK_COUNT][4]=board[BOARD_COUNT+1][BOARD_COUNT+1]
blocks[BLOCK_COUNT][5]=board[BOARD_COUNT+1][BOARD_COUNT+2]
blocks[BLOCK_COUNT][6]=board[BOARD_COUNT+2][BOARD_COUNT]
blocks[BLOCK_COUNT][7]=board[BOARD_COUNT+2][BOARD_COUNT+1]
blocks[BLOCK_COUNT][8]=board[BOARD_COUNT+2][BOARD_COUNT+2]
BLOCK_COUNT+=1
BOARD_COUNT+=3
但是,这给了我一个索引错误.如果我用"BLOCK_COUNT"分别是3和6创建了两个while循环,那么我会得到更好的答案,但是对于某些人来说,它仍然不能提供正确的3x3块.因此,我对于如何执行此操作非常不知所措.谢谢.
This however gives me an index error. if I create 2 of those while loops with "BLOCK_COUNT" being 3 and 6 respectively then i get a better answer but it still doesn't give me the correct 3x3 block for some. So i'm pretty much at a loss for how to do this. Thanks.
推荐答案
def getBlocks(board):
answer = []
for r,c in itertools.product(range(3), repeat=2):
answer.append([board[r+i][c+j] for i,j in itertools.product(range(0, 9, 3), repeat=2)])
return answer
当然,您可以只用一个列表理解来替换整个内容:
Of course, you could replace the whole thing with just one list comprehension:
answer = [[board[r+i][c+j] for i,j in itertools.product(range(0, 9, 3), repeat=2)]
for r,c in itertools.product(range(3), repeat=2)]
如果您对不使用任何内置功能进行繁重工作的版本感兴趣:
In case you are interested in a version that doesn't use any built-ins to do any heavy lifting:
def getBlocks(board):
answer = []
for r in range(3):
for c in range(3):
block = []
for i in range(3):
for j in range(3):
block.append(board[3*r + i][3*c + j])
answer.append(block)
return answer
那么这是怎么回事?:
首先,我们决定迭代所需的9个块.这些由r
和c
变量控制.这也是为什么在访问板上的数字时将它们乘以3的原因(因为每个块都是3边的平方).
Well, first, we decide to iterate over the 9 blocks that we want. These are governed by the r
and c
variables. This is also why we multiply them by 3 when we access the numbers on the board (because each block is a square of side 3).
接下来,我们要遍历每个块中的元素.翻译:查找每个3x3块中的数字.块中每个元素的索引由i
和j
控制.因此,我们有i
和j
来控制我们要访问的元素,以及r
和c
(它们与电路板本身的偏移量),确定我们想要的块"的位置.现在我们开始比赛了.
Next, we want to iterate over the elements in each block. Translation: Lookup the numbers within each 3x3 block. The index of each element within the block is governed by i
and j
. So we have i
and j
that govern the elements we want to access, along with r
and c
, which are their offsets from the board itself, determining the location of the "block" we want. Now we're off to the races.
对于每个r
和c
(请注意,每个循环遍历range(3)
,所以有9对(r,c)
对-我们后面的9个块),循环遍历该块中的9个元素( 9个(i,j)
对).现在,只需根据相对位置从(r,c)
偏移量访问元素即可(3*r
给出相关块的第一行,添加i
给出所需元素的行.类似地,3*c
给出相关块的第一列,并添加j
给出所需元素的列.因此,我们有了所需元素的坐标).现在,我们将元素添加到block
.
For each r
and c
(notice that each loops over range(3)
, so there are 9 (r,c)
pairs - the 9 blocks that we are after), loop over the 9 elements in the block (the 9 (i,j)
pairs). Now, simply access the elements based on their relative locations from the (r,c)
offsets (3*r
gives the first row of the relevant block, and adding i
gives the row of the required element. Similarly, 3*c
gives the first column of the relevant block, and adding j
gives the column of the required element. Thus, we have the coordinates of the element we want). Now, we add the element to block
.
一旦我们遍历了块中的所有元素,就将块本身添加到答案中,然后保存!我们完成了
Once we've looped over all the elements in the block, we add the block itself to the answer, and presto! we're done
这篇关于如何在python中创建3x3数独块列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!