Convert a two dimensional array(length 9) to a Grids of 3 x 3 (9 elements)

P

Prabhanjan

Guest
I have a 2-D array which represent a series of row values in a table shown below:

[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]


I needed to get column values from the same and I was able to do that using

map(list, zip(*arr))
and got this:

[
['5', '6', '.', '8', '4', '7', '.', '.', '.'],
['3', '.', '9', '.', '.', '.', '6', '.', '.'],
['.', '.', '8', '.', '.', '.', '.', '.', '.'],
['.', '1', '.', '.', '8', '.', '.', '4', '.'],
['7', '9', '.', '6', '.', '2', '.', '1', '8'],
['.', '5', '.', '.', '3', '.', '.', '9', '.'],
['.', '.', '.', '.', '.', '.', '2', '.', '.'],
['.', '.', '6', '.', '.', '.', '8', '.', '7'],
['.', '.', '.', '3', '1', '6', '.', '5', '9']
]



Now I need to create a similar two dimensional array that contains 3 x 3 grid elements and the output should look like below

[
['5','3','.','6','.','.','.','9','8'],
['.','7','.','1','9','5','.','.','.'],
['.','.','.','.','.','.','.','6','.'],
['8','.','.','4','.','.','7','.','.'],
['.','6','.','8','.','3','.','2','.'],
['.','.','3','.','.','1','.','.','6'],
['.','6','.','.','.','.','.','.','.'],
['.','.','.','4','1','9','.','8','.'],
['2','8','.','.','.','5','.','7','9']
]


I tried to zip 3 elements together and iterate 3 rows but I got nothing

x = 3
for i in range(x):
map(list, zip(*arr)[0,1,2])
x += 3

Continue reading...
 
Top