Array Indexing
Accessing elements in the array is similar to accessing elements in a Python list:
print(r1[0]) # 1print(r1[1]) # 2
The following code snippet creates another array named r2, which is two‐dimensional:
list2 = [6,7,8,9,0]r2 = np.array([list1,list2]) # rank 2 arrayprint(r2)'''[[1 2 3 4 5][6 7 8 9 0]]'''print(r2.shape) # (2,5) - 2 rows and 5 columnsprint(r2[0,0]) # 1print(r2[0,1]) # 2print(r2[1,0]) # 6
Here, r2 is a rank 2 array, with two rows and five columns.
Besides using an index to access elements in an array, you can also use a list as the index as follows:
list1 = [1,2,3,4,5]r1 = np.array(list1)print(r1[[2,4]]) # [3 5]
Boolean Indexing
In addition to using indexing to access elements in an array, there is another very cool way to access elements in a NumPy array. Consider the following:
print(r1>2) # [False False True True True]...