Hi William,

You can simply use a for loop for that task:

Python 3.8.2 | packaged by conda-forge | (default, Apr 24 2020, 07:56:27)

IPython 7.14.0 -- An enhanced Interactive Python. Type '?' for help.

In : import numpy as np

In : a = np.arange(3).reshape((1, 3))

In : for x in a.T:

...:     print(x)

...:







Best regards,

Hameer Abbasi

From: NumPy-Discussion <numpy-discussion-bounces+einstein.edison=gmail.com@python.org> on behalf of William Ayd <william.ayd@icloud.com>
Reply to: Discussion of Numerical Python <numpy-discussion@python.org>
Date: Tuesday, 19. May 2020 at 01:42
To: "numpy-discussion@python.org" <numpy-discussion@python.org>
Subject: Re: [Numpy-discussion] Using nditer + external_loop to Always Iterate by Column

I am trying to use the nditer to traverse each column of a 2D array, returning the column as a 1D array. Consulting the docs, I found this example which works perfectly fine:

In : a = np.arange(6).reshape(2,3)

In : for x in np.nditer(a, flags=['external_loop'], order='F'):

...:     print(x, end=' '

...:

[0 3] [1 4] [2 5]

When changing the shape of the input array to (1, 3) however, this doesn’t yield what I am hoping for any more (essentially ,  ):

In : for x in np.nditer(a, flags=['external_loop'], order='F'):

...:     print(x, end=' '

...:

[0 1 2]

I suspect this may have to do with the fact that the (1, 3) array is both C and F contiguous, and it is trying to return as large of a 1D F-contiguous array as it can. However, I didn’t see any way to really force it to go by columns. My best guess was the itershape argument though I couldn’t figure out how to get that to work and didn’t see much in the documentation.

Thanks in advance for the help!

- Will