2

I want to convert this array

[array([46, 64, 50, 66]),
 array([53, 61, 59, 59]),
 array([54, 63, 55, 61]),
 array([56, 58, 51, 55])]

into this array

[array([46, 53, 54, 56]),
 array([64, 61, 63, 58]),
 array([50, 59, 55, 51]),
 array([66, 59, 61, 55])]

Is there a way to do this in numpy?

4
  • Explain what exactly you want to do, instead of providing just an example. Commented Mar 9, 2021 at 12:03
  • Although you're looking for the transposition operator. Commented Mar 9, 2021 at 12:03
  • @user202729, Exactly. I want to rearrange this array. Commented Mar 9, 2021 at 12:04
  • Mostly duplicate of python - How to flip numpy array along the diagonal efficiently? - Stack Overflow , although the answers there are terrible. Commented Mar 9, 2021 at 12:11

2 Answers 2

6

Numpy allows you to transpose. Cast the list to numpy array and use .T

import numpy as np

case = [np.array([46, 64, 50, 66]),
 np.array([53, 61, 59, 59]),
 np.array([54, 63, 55, 61]),
 np.array([56, 58, 51, 55])]

# transform `[ ]` list to array and then `.T`
np.array(case).T # Transpose

See documentation of Transpose for more details.

Sign up to request clarification or add additional context in comments.

Comments

1

check out this python docs link for help Check for transpose function in numpy as follows https://numpy.org/doc/stable/reference/generated/numpy.transpose.html

1 Comment

@user202729 Thanks. This was really helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.