views:

49

answers:

1

I have a 3d array, a, of shape say a.shape = (10, 10, 10)

When slicing, the dimensions are squeezed automatically i.e.

a[:,:,5].shape = (10, 10)

I'd like to preserve the number of dimensions but also ensure that the dimension that was squeezed is the one that shows 1 i.e.

a[:,:,5].shape = (10, 10, 1)

I have thought of re-casting the array and passing ndmin but that just adds the extra dimensions to the start of the shape tuple regardless of where the slice came from in the array a.

+4  A: 
a[:,:,[5]].shape
# (10,10,1)
unutbu
Thanks, however I should add that I am implementing a `__getitem__` method and so I'm reluctant to alter the slice arguments - see the edit
Brendan
numpy's rules for indexing are complicated enough. Creating a `__getitem__` that alters those rules may be asking for trouble. Wouldn't it be better to demand users of your object understand numpy's rules and pass the correct index?
unutbu