0

I have the following code:

areas=[1,2,3,4,5,6,7,8,9]
filas=np.sqrt(len(areas))
i=0
matriz=[]
while i < filas:
    globals()['L%s'%(i+1)]=areas[(len(areas)//filas)*i:(len(areas)//filas)*(i+1)]
    matriz.append('L%s'%(i+1))
    i+=1

This just gives me:

matriz=['L1','L2','L3']

What I want to get is:

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

I wanted something general because actually I have to do this in a list of 1600 elements, so I will have up to L40 lists in my list "matriz".

  • 2
    How does `L` enter into this? – Stephen Rauch Mar 31 '18 at 21:57
  • With `globals()['L%s'%(i+1)]=areas[(len(areas)//filas)*i:(len(areas)//filas)*(i+1)]` I'm creating lists like `L1 = [1,2,3]; L2 = [4,5,6]; L3 = [7,8,9]` – Ritzion Mar 31 '18 at 21:59
  • @StephenRauch The extremely questionable `globals` usage. – miradulo Mar 31 '18 at 21:59
  • 1
    Sorry for that, I know it's horrible to look at, I have like 0 experience... I would appreciate ways to improve my code though. – Ritzion Mar 31 '18 at 22:03
  • What you are trying to do is common for newer programmers. But it is almost always a bad idea. The duplicate link contains links to other duplicates. Lots available to read. – Stephen Rauch Mar 31 '18 at 22:04
  • Oh, okay, sorry for the duplicate, I swear I spent a lot of time trying to find what I posted, thank you for the information. I'll read what you suggest. – Ritzion Mar 31 '18 at 22:10

0 Answers0