Note
Click here to download the full example code
12.3.2.3. Indexing, Slicing and IteratingΒΆ
import numpy as np
# **One dimensional**
a = np.arange(10)**3
a[2]
Out:
8
a[2:5]
Out:
array([ 8, 27, 64], dtype=int32)
a[:6:2] = 1000
reverse array
a[::-1]
Out:
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
Out:
array([[ 0, 1, 2, 3],
[10, 11, 12, 13],
[20, 21, 22, 23],
[30, 31, 32, 33],
[40, 41, 42, 43]])
b[2, 3]
Out:
23
b[0:5, 1]
Out:
array([ 1, 11, 21, 31, 41])
b[:, 1]
Out:
array([ 1, 11, 21, 31, 41])
b[1:3, :]
Out:
array([[10, 11, 12, 13],
[20, 21, 22, 23]])
b[-1]
Out:
array([40, 41, 42, 43])
Total running time of the script: ( 0 minutes 0.005 seconds)