3
votes

In numpy doc about basic slicing, there is a situation described as:

basic slicing is also initiated if the selection object is any non-ndarray sequence (such as a list) containing slice objects, the Ellipsis object, or the newaxis object, but not for integer arrays or other embedded sequences.

It's hard to get the idea of this description. When does this situation happen? Can you show me some examples for this situation?

1

1 Answers

3
votes

This is an interesting convenience. Lets say that I have the following array:

arr = np.arange(100)

If I'm being silly, I can slice an array like this:

arr[:30, ..., np.newaxis]

(The Ellipsis doesn't do anything helpful here, but I can put it on there). Basically this tells basic slicing to take the first 30 elements and then add a new axis.

If I'd rather (or if it's more convenient for whatever reason), I can accomplish the same slicing like this:

arr[[slice(0, 30), Ellpisis, np.newaxis]]

If I have a 2d array:

arr = arr.reshape((10, 10))

I can have multiple slice objects:

arr[[slice(0, 5), slice(5, 10, 2)]]

So I suppose that in general, you can think of arr[[x_0, x_1, x_2, ..., x_N]] as equivalent to arr[x_0, x_1, x_2, ..., x_N] so long as x_0, ... x_N are all slices, ellipsis, or None.