How to break a list and transform into array in Python?

2

In case, I want to transform a list of a column into a 3x3 array in numerical order:

lista =([[1],
     [2],
     [3],
     [4],
     [5],
     [6],
     [7],
     [8],
     [9]])

Being the expected result:

([[7 8 9]
 [4 5 6]
 [1 2 3]])
    
asked by anonymous 16.04.2018 / 03:49

1 answer

2

Using numpy:

import numpy as np
lista = np.arange(1, 10).reshape(9, 1)
print(lista)
'''
array([[1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9]])
'''

Now we can do:

lista = lista.reshape(3, 3)[::-1] # forma-lo em 3 linhas e 3 colunas, e inverter
print(lista)
'''
array([[7, 8, 9],
       [4, 5, 6],
       [1, 2, 3]])
'''

DEMONSTRATION

    
16.04.2018 / 08:44