python/2D_int_array_indexing.py

20 lines
453 B
Python

import numpy as np
array_2d = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
# Retrieving first and the third row
print(array_2d[[0, 2]])
# Retrieving the main diagonal elements
print(array_2d[[0, 1, 2], [0, 1, 2]])
# Retrieving the first and third element of the second row
print(array_2d[1, [0, 2]])
# IndexError is thrown, since index 3 along axis 0 is out of bounds
print(array_2d[[0, 2], [0, 1]])
#print(array_2d[[0, 1, 2], [2, 1, 0]])