A NumPy array is a data structure that can be used to store single or multi-dimensional arrays or matrices. Those arrays can later be used for advanced mathematical operations like matrix operations. As we discussed in our previous article, a NumPy array can be created from a Python list. In Python, we can use the index of an array element to access the element. For example,
import numpy list1 = [1, 2, 3] list2 = [[1, 2, 3], [4, 5, 6]] list3 = [[1, 2, 3]] array1 = numpy.array(list1) array2 = numpy.array(list2) array3 = numpy.array(list3) print(array1[0]) print(array2[0][1]) print(array3[0][2])
The output of the above program will be like the following:
1 2 3
Here, we are first creating three lists list1, list2 and list3. Then, we are using the numpy.array() function to convert the lists into arrays. Please note that list2 and list3 are list of lists. So, list1 creates a one-dimensional array. The lists list2 and list3 create two-dimensional arrays each.
So, we are using array1[0] to access the first element of the array array1. To access the element at the 1st row and 2nd column of array2, we need to use array2[0][1]. Please note that indexes start from 0, and not from 1.
Similarly, if we want to access the element in the first row and 3rd column of array3, we need to use array3[0][2].






0 Comments