1

My confusion arises from the following example:

import numpy as np

a = np.array([['A', 'B', 'C', 'D', 'E'],
       ['F', 'G', 'H', 'I', 'J'],
       ['K', 'L', 'M', 'N', 'O'],
       ['P', 'Q', 'R', 'S', 'T'],
       ['U', 'V', 'W', 'X', 'Y']])

print(a[:2, 1:4])
print()
print(a[:2][1:4])

Output:

[['B', 'C', 'D'],
 ['G', 'H', 'I']]

[['F', 'G', 'H', 'I', 'J']]

x = np.arange(10)
x.shape = (2,5)

In this example given by Numpy.org in the Single element indexing section, x[0,2] = x[0][2], so I assumed this would hold for the above.

I've tried the above with other examples such as y = np.arange(35).reshape(5,7) with y[1:5:2,::3] and y[1:5:2][::3]. The output is different: I think this does not hold when slices are used.

0

1 Answer 1

2

First of all, 2D ndarray can be sliced as a[row slice, col slice]. So, x[:2, 1:4] is to slice ndarray x based on both row slice ([:2) and column slice ([1:4]).

However, x[:2][1:4] is slice [:2] first, and then slice [1:4]. Thus, x[:2][1:4] is the same as x[1:2].

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.