Take 1 list of lists and make x the number of squares, where x is the size of the list in python

So, I am working on a project for a class, it calls to create a list where you say 9x9 square, and create a new list where there will be 9 3x3 sets.

list of examples:

L = [[5,3,4,6,7,8,9,1,2],
     [6,7,2,1,9,5,3,4,8],
     [1,9,8,3,4,2,5,6,7],
     [8,5,9,7,6,1,4,2,3],
     [4,2,6,8,5,3,7,9,1],
     [7,1,3,9,2,4,8,5,6],
     [9,6,1,5,3,7,2,8,4],
     [2,8,7,4,1,9,6,3,5],
     [3,4,5,2,8,6,1,7,9]]

so I want my new list to be:

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

Hope you see the drawing.

After 5 hours, I was mainly able to recreate the list with the list:

List = [[row[col] for col in range(len(list1))] for row in  list1]

then the closest I could get:

List = [[row[col] for col in range(int(sqrtofsize)] for row in  list1]

where it will print the first 3 of each element, then I, although I can create 3 separate lists, the only problem is that you have a 16x16 block that it will not work.

Another problem is that I cannot make it less clear.

I am sure that there is information, although I was looking for 5 hours, and I just can not figure it out.

please clarify.

thanks

+4
2

:

newL = [[L[3*(i//3)+j//3][3*(i%3)+j%3] for j in range(9)] for i in range(9)]

:

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

: http://ideone.com/XSHhLj

3x3 :

L = [[1,2,3],
     [4,5,6],
     [7,8,9]]
iterL = [ L[i//3][i%3] for i in range(9) ]

:

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

, 3 ( i//3) 0 ( i%3). , i=4 1,0.

, . =)

(, ), 3 9 .

+1
def subsquare(L, N, x, y):
  return [ [ L[i][j] for j in range(N*y, N*y+N) ] for i in range(N*x, N*x+N) ]

N = 3

L = [[5,3,4,6,7,8,9,1,2],
     [6,7,2,1,9,5,3,4,8],
     [1,9,8,3,4,2,5,6,7],
     [8,5,9,7,6,1,4,2,3],
     [4,2,6,8,5,3,7,9,1],
     [7,1,3,9,2,4,8,5,6],
     [9,6,1,5,3,7,2,8,4],
     [2,8,7,4,1,9,6,3,5],
     [3,4,5,2,8,6,1,7,9]]

subsqaure(L, N, 0, 0)
[[5, 3, 4], [6, 7, 2], [1, 9, 8]]

for x in range(N):
   for y in range(N):
       print x, y, subsquare(L, N, x, y)


 0 0 [[5, 3, 4], [6, 7, 2], [1, 9, 8]]
 0 1 [[6, 7, 8], [1, 9, 5], [3, 4, 2]]
 0 2 [[9, 1, 2], [3, 4, 8], [5, 6, 7]]
 1 0 [[8, 5, 9], [4, 2, 6], [7, 1, 3]]
 1 1 [[7, 6, 1], [8, 5, 3], [9, 2, 4]]
 1 2 [[4, 2, 3], [7, 9, 1], [8, 5, 6]]
 2 0 [[9, 6, 1], [2, 8, 7], [3, 4, 5]]
 2 1 [[5, 3, 7], [4, 1, 9], [2, 8, 6]]
 2 2 [[2, 8, 4], [6, 3, 5], [1, 7, 9]]
+2

All Articles