Array to list with sublists

0

I am trying to convert an Array to a list of sublists, such that:

input: '004013087631908524907452613506180379198735046740200801375849102802367495060021030'

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

this is what I managed to do

def arr2sud(l):
   y = [l[i:i+9] for i in range(0, len(l), 9)]
   f = [[],[],[],[],[],[],[],[],[]]
   for i in range(len(y)):
      d = ",".join(y[i])
      f[i]=d
   return f

but the output is: ['0,0,4,0,1,3,0,8,7', '6,3,1,9,0,8,5,2,4', '9,0,7,4,5,2,6,1,3', '5,0,6,1,8,0,3,7,9', '1,9,8,7,3,5,0,4,6', '7,4,0,2,0,0,8,0,1', '3,7,5,8,4,9,1,0,2', '8,0,2,3,6,7,4,9,5', '0,6,0,0,2,1,0,3,0']

That is, it returns a list of arrays. I know it can be done with numpy, but I do not want to use it. Thanks!

    
asked by miguel medina 03.02.2017 в 15:03
source

2 answers

0

You need one more loop, which iterates over f , example:

def arr2sud(l):
   y = [l[i:i+9] for i in range(0, len(l), 9)]
   f = [[],[],[],[],[],[],[],[],[]]

   for q in range(len(f)):
    for i in range(len(y)):
      d = ",".join(y[i])
   f[q]=d
return f
    
answered by 03.02.2017 в 16:00
0

Something a little convoluted in a line:

x = '004013087631908524907452613506180379198735046740200801375849102802367495060021030'
result = [list(map(int, list(x[i:i+9]))) for i in range(0, len(x), 9)]
    
answered by 03.02.2017 в 16:02