0

for instance converting [ 3, 1, 2, 4, 4, 2, 3, 1, 1, 3, 4, 2, 2, 4, 1, 3 ] to

[
      [3, 1, 2, 4],
      [4, 2, 3, 1],
      [1, 3, 4, 2],
      [2, 4, 1, 3]
]

shortest method ... not converting it into 4 lists then to 2d

1 Answers1

0

In numpy you can convert a 1d array to a 2d array by using the reshape command. The first argument is your current array, and the second one specifies what the new 'shape' of it should look like. In this case, 4 rows by 4 columns.

import numpy as np

my_array = [ 3, 1, 2, 4, 4, 2, 3, 1, 1, 3, 4, 2, 2, 4, 1, 3 ]
new_array = np.reshape(my_array, (4, 4))

will result in new_array being:

array([[3, 1, 2, 4],
       [4, 2, 3, 1],
       [1, 3, 4, 2],
       [2, 4, 1, 3]])

Akavall was kind enough to link directly to the docs page on the reshape command, copying here: https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html

CasualDemon
  • 5,790
  • 2
  • 21
  • 39