What is the numpy equivalent of python zip (*)?

I think (hopefully) this question is significantly different from What is the equivalent of "zip ()" in numpy Python? although it may just be my ignorance.

Say I have the following:

 [[[ 12],
   [3, 4],
   [5, 6]],
  [[7, 8],
   [9, 10],
   [11, 12]]]

and I want to turn it into

 [[[ 12],
   [7, 8]],
  [[3, 4],
   [9, 10]],
  [[5, 6],
   [11, 12]]]

In python, I can do:

>>> foo [[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]] >>> zip(*foo) [([1, 2], [7, 8]), ([3, 4], [9, 10]), ([5, 6], [11, 12])] 

But how to do it with numpy arrays (without using zip (*))?

+8
python numpy zip
source share
1 answer

Do you really need to return tuples or do you want to change the shape of the array?

 >>> a array([[[ 1, 2], [ 3, 4], [ 5, 6]], [[ 7, 8], [ 9, 10], [11, 12]]]) >>> a.swapaxes(0,1) array([[[ 1, 2], [ 7, 8]], [[ 3, 4], [ 9, 10]], [[ 5, 6], [11, 12]]]) 
+6
source share

All Articles