
Using Arrays with Numpy 251
17.5.4 Additional Vector Operations
In addition to slicing, selective indexing can also be carried out using arrays
of indices. The following program stored in file test9
arrays.py creates an
array of indices in line 6 and applies it to array p to select the corresponding
elements in line 7 . T he se selected e le ments are assigned the value 2.0 in line
9.
1 import numpy as np
2 print "Array of indices"
3 p = np.array([1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5])
4 print "Array p: "
5 print p
6 pindx = np.array([1, 4, 6, 7])
7 q = p[pindx]
8 print "q: ", q
9 p[pindx] = 2.0
10 print "Array p:"
11 print p
Executing the Python interpreter ...