Perform the reshape of different arrays

Asked

Viewed 54 times

-1

Hello!

Follow down my doubt:

array_a = [1,2,3]
array_b = [1,2,3]
array_c = [1,2,3]

I’d like them to look like this:

array_reshaped = 
  [[1, 1, 1],
   [2, 2, 2],
   [3, 3, 3]]

Could I perform this operation in python using the numpy.reshape?

  • 2

    It is not enough just to calculate the transposed matrix?

  • 2

    The zip alone does not serve?

1 answer

2


What you need is the transposed matrix, not a reshape.

>>> matrix = numpy.matrix(
...   [[1, 2, 3],
...    [1, 2, 3],
...    [1, 2, 3]]
... )

To get the transposed matrix just do matriz.T:

>>> print(matrix.T)
[[1 1 1]
 [2 2 2]
 [3 3 3]]

If you use Numpy only for this, forget it. As commented, the function itself zip already solves the problem:

result = [lista for lista in zip(array_a, array_b, array_c)]
  • Thank you very much, man, you helped me a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.