Note
Go to the end to download the full example code.
12.3.10.2.4. Indexing, Slicing and Iterating#
import numpy as np
# **One dimensional**
a = np.arange(10) ** 3
a[2]
8
a[2:5]
array([ 8, 27, 64], dtype=int32)
a[:6:2] = 1000
reverse array
a[::-1]
array([ 729, 512, 343, 216, 125, 1000, 27, 1000, 1, 1000],
dtype=int32)
Multi dimensional
def f(x, y):
return 10 * x + y
b = np.fromfunction(f, (5, 4), dtype=int)
b
array([[ 0, 1, 2, 3],
[10, 11, 12, 13],
[20, 21, 22, 23],
[30, 31, 32, 33],
[40, 41, 42, 43]])
b[2, 3]
23
b[0:5, 1]
array([ 1, 11, 21, 31, 41])
b[:, 1]
array([ 1, 11, 21, 31, 41])
b[1:3, :]
array([[10, 11, 12, 13],
[20, 21, 22, 23]])
b[-1]
array([40, 41, 42, 43])
Total running time of the script: (0 minutes 0.010 seconds)