Let's say I have a numpy array. When using a single advanced index, the behavior is as expected.
>>> import numpy as np
>>> A = np.random.rand(4, 4, 4)
>>> A[:, :, 0].shape
(4, 4)
>>> A[:, :, [0, 1]].shape
(4, 4, 2)
However, when another index is added, the order of the dimensions are changed.
>>> A[0, :, [0, 1]].shape
(2, 4)
Interestingly enough, when using a slice instead of an single index, the original array ordering is preserved
>>> A[0:1, :, [0, 1]].shape
(1, 4, 2)
Thus if I need to preserve the dimensions of A, I must do two indexing operations or transpose the result.
>>> A[0, :, :][:, [0, 1]].shape
(4, 2)
After a bit of googling I found a page in the numpy manual describing what is going on.
To summarize the section on advanced indexing, apparently the 1
is treated as an advanced index. Because the advanced indexes are not next to each other, there is no "unambiguous place" to put the dimension, thus it is tacked-on to the beginning.
So why is a single number treated as an advanced index when combined with other advanced indices, while a slice is not?
I understand why using multiple lists or arrays as indices would make it ambiguous to know where to place the extra dimensions, but why would it be ambiguous when one of them is a list and the other is a number?