IndexingΒΆ
One-dimensional arrays can be indexed using the same syntax and techniques we used for lists. Here, we focus on array-specific indexing capabilities.
Here is what it looks like to index a one-dimensional array:
import numpy as np
arr = np.array(['A', 'B', 'C'])
print(arr[0]) # A
arr[2] = 'Z' # Assigns the value at index 2 to Z
Here is what it looks like to index a two-dimensional array:
import numpy as np
arr = np.array([['A', 'B', 'C'],['D', 'E', 'F']])
print(arr[0][0]) # Row 0 , Column 0
arr[1][0] = 'Z' # Assigns the value at row 1 and column 0 (D) to Z
You can use loops to index arrays of all dimensions, like so:
import numpy as np
arr = np.array(['A', 'B', 'C', 'D', 'E', 'F']) # One-dimension
for i in arr:
print(i)
arr2 = np.array([[1, 2, 3], [4, 5, 6]]) # Two-dimensions
for i in arr2:
for j in i: # Notice that i is the iterable used in the inner loop
print(j)