Detailed explanation of Python two dimensional array and three dimensional array slicing method

  • 2021-07-22 10:02:30
  • OfStack

If the object is a 2-dimensional array, the slice should be in the form of x [:], with a colon, preceded by and followed by the 0th dimension and the first dimension of the object, respectively;

If the object is a 3-dimensional array, the slice should be x [::], with two colons in it, dividing three intervals, and the front, middle and back of the three intervals represent the 0th, 1st and 2nd dimensions of the object respectively.

x [n,:], x [:, n], x [m: n,:], x [:, m: n]

The above brackets (m: n) should be considered as a whole, and colons other than (m: n) are used to indicate which dimension to operate on.

For 2-D arrays, the (n,) preceding the colon means operating on element n on dimension 0 of the 2-D array, and the (, n) following the colon means operating on element n on dimension 1 of the 2-D array. If n is replaced by (m: n), it means operation on elements m through n-1.

Examples:


import numpy as np

a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]])
print(a.shape)
print(a[0, :], a[0, :].shape)
print(a[1, :], a[1, :].shape)
print(a[-1, :], a[-1, :].shape)
print(a[0:2, :], a[0:2, :].shape)
print(a[:, 0], a[:, 0].shape)
print(a[:, 1], a[:, 1].shape)
print(a[:, -1], a[:, -1].shape)
print(a[:, 0:2], a[:, 0:2].shape)

The running results are as follows:


(5, 4)
[1 2 3 4] (4,)
[5 6 7 8] (4,)
[17 18 19 20] (4,)
[[1 2 3 4]
 [5 6 7 8]] (2, 4)
[ 1 5 9 13 17] (5,)
[ 2 6 10 14 18] (5,)
[ 4 8 12 16 20] (5,)
[[ 1 2]
 [ 5 6]
 [ 9 10]
 [13 14]
 [17 18]] (5, 2)

Process finished with exit code 0

In the above example, a is an array of shape= (5, 4). There are 5 elements in Dimension 0 and 4 elements in Dimension 1 (Element 1 is either a single value or an array, where elements are called relative to a certain dimension).

a [0,:], a [1,:], a [-1,:] extract elements 0, 1 and-1 on dimension 0 of a, each of which is an array of 4 elements. a [0: 2,:] extracts two elements 0 and 1 in dimension 0 of a, both of which are an array of four elements and together form a 2-dimensional array. a [:, 0], a [:, 1], a [:,-1] extract 0, 1 and-1 elements on the first dimension of a, respectively, and each element is a single element value. a [:, 0: 2] extracts two elements 0 and 1 on the first dimension of a, both of which are single element values and together form a 2-dimensional array.

x [n,::], x [:, n:], x [::, n], x [:,:, n], x [m: n,::], x [:, m: n:], x [::, m: n], x [:,:, m: n]

The above brackets (m: n) should be considered as a whole, and the two colons except (m: n) are used to indicate which dimension to operate on.

For 3-D arrays, the preceding (n,) of the double colon means operating on element n on dimension 0 of the 3-D array, the middle (, n) means operating on element n on dimension 1 of the 3-D array, and the following (, n) means operating on element n on dimension 2 of the 3-D array. If n is replaced by (m: n), it means operation on elements m to n-1.

Examples:


import numpy as np

b = np.array([[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]],
       [[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]],
       [[25, 26, 27, 28], [29, 30, 31, 32], [33, 34, 35, 36]],
       ])

print(b.shape)
print("b[0, ::],b[1, ::],b[-1, ::],b[0:2, ::]")
print(b[0, ::], b[0, ::].shape)
print(b[1, ::], b[1, ::].shape)
print(b[-1, ::], b[-1, ::].shape)
print(b[0:2, ::], b[0:2, ::].shape)
print("b[:, 0:],b[:, 1:],b[:, -1:],b[:, 0:2:]")
print(b[:, 0:], b[:, 0:].shape)
print(b[:, 1:], b[:, 1:].shape)
print(b[:, -1:], b[:, -1:].shape)
print(b[:, 0:2:], b[:, 0:2:].shape)
print("b[::, 0],b[::, 1],b[::, -1],b[::, 0:2:]")
print(b[::, 0], b[::, 0].shape)
print(b[::, 1], b[::, 1].shape)
print(b[::, -1], b[::, -1].shape)
print(b[::, 0:2:], b[::, 0:2].shape)
print("b[:,:, 0],b[:,:, 1],b[:,:, -1],b[:,:, 0:2:]")
print(b[:, :, 0], b[:, :, 0].shape)
print(b[:, :, 1], b[:, :, 1].shape)
print(b[:, :, -1], b[:, :, -1].shape)
print(b[:, :, 0:2:], b[:, :, 0:2].shape)

The running results are as follows:


(3, 3, 4)
b[0, ::],b[1, ::],b[-1, ::],b[0:2, ::]
[[ 1 2 3 4]
 [ 5 6 7 8]
 [ 9 10 11 12]] (3, 4)
[[13 14 15 16]
 [17 18 19 20]
 [21 22 23 24]] (3, 4)
[[25 26 27 28]
 [29 30 31 32]
 [33 34 35 36]] (3, 4)
[[[ 1 2 3 4]
 [ 5 6 7 8]
 [ 9 10 11 12]]

 [[13 14 15 16]
 [17 18 19 20]
 [21 22 23 24]]] (2, 3, 4)
b[:, 0:],b[:, 1:],b[:, -1:],b[:, 0:2:]
[[[ 1 2 3 4]
 [ 5 6 7 8]
 [ 9 10 11 12]]

 [[13 14 15 16]
 [17 18 19 20]
 [21 22 23 24]]

 [[25 26 27 28]
 [29 30 31 32]
 [33 34 35 36]]] (3, 3, 4)
[[[ 5 6 7 8]
 [ 9 10 11 12]]

 [[17 18 19 20]
 [21 22 23 24]]

 [[29 30 31 32]
 [33 34 35 36]]] (3, 2, 4)
[[[ 9 10 11 12]]

 [[21 22 23 24]]

 [[33 34 35 36]]] (3, 1, 4)
[[[ 1 2 3 4]
 [ 5 6 7 8]]

 [[13 14 15 16]
 [17 18 19 20]]

 [[25 26 27 28]
 [29 30 31 32]]] (3, 2, 4)
b[::, 0],b[::, 1],b[::, -1],b[::, 0:2:]
[[ 1 2 3 4]
 [13 14 15 16]
 [25 26 27 28]] (3, 4)
[[ 5 6 7 8]
 [17 18 19 20]
 [29 30 31 32]] (3, 4)
[[ 9 10 11 12]
 [21 22 23 24]
 [33 34 35 36]] (3, 4)
[[[ 1 2 3 4]
 [ 5 6 7 8]]

 [[13 14 15 16]
 [17 18 19 20]]

 [[25 26 27 28]
 [29 30 31 32]]] (3, 2, 4)
b[:,:, 0],b[:,:, 1],b[:,:, -1],b[:,:, 0:2:]
[[ 1 5 9]
 [13 17 21]
 [25 29 33]] (3, 3)
[[ 2 6 10]
 [14 18 22]
 [26 30 34]] (3, 3)
[[ 4 8 12]
 [16 20 24]
 [28 32 36]] (3, 3)
[[[ 1 2]
 [ 5 6]
 [ 9 10]]

 [[13 14]
 [17 18]
 [21 22]]

 [[25 26]
 [29 30]
 [33 34]]] (3, 3, 2)

Process finished with exit code 0

In the above example, b is an array of shape= (3, 3, 4). There are 3 elements in Dimension 0, 3 elements in Dimension 1, and 4 elements in Dimension 2 (Element 1 is either a single value or an array, where elements are called relative to a certain dimension).

b [0,::], b [1,::], b [-1,::] Extract elements 0, 1 and-1 on dimension 0 of b, each of which is a 2-dimensional array. b [0: 2,::] extracts two elements 0 and 1 in dimension 0 of b, both of which are a 2-dimensional array and together form a 3-dimensional array. b [:, 0:], b [:, 1:], b [:,-1:] respectively extract all elements of b (all single arrays of 4 elements), all elements except 0 in the first dimension of b (all single arrays of 4 elements), and all elements in the last 1 position in the first dimension of b (all single arrays of 4 elements). b [:, 0: 2:] extracts two elements 0 and 1 in the first dimension of b, both of which are an array with four elements, and together constitute a 3-dimensional array. b [::, 0], b [::, 1], b [::,-1] extract the 0, 1 and-1 elements of the second dimension of b, respectively (the element here is a single array with four elements), each of which is an array with four elements. b [::, 0: 2] extracts Elements 0 and 1 from Dimension 2 of b (where the element is a single 4-element array), both of which are 4-element arrays. b [:,:, 0], b [:,:, 1], b [:,:,-1] extract element values 0, 1 and-1 from all elements of dimension 2 of b (that is, an array of 4 elements), each of which is a single element value. b [:,:, 0: 2] extracts both element values 0 and 1 from all elements (that is, an array of 4 elements) in dimension 2 of b, both of which are single element values.

Related articles: