- AI Crash Course
- Hadelin de Ponteves
- 552字
- 2021-03-26 16:22:44
Lists and arrays
Lists and arrays can be represented with a table. Imagine a one-dimensional (1D) vector or a matrix, and you have just imagined a list/array.
Lists and arrays can contain data in them. Data can be anything – variables, other lists or arrays (these are called multi-dimensional lists/arrays), or objects of some classes (we will learn about them later).
For example, this is a 1D list/array containing integers:
And this is an example of a two-dimensional (2D) list/array, also containing integers:
In order to create a 2D list, you have to create a list of lists. Creating a list is very simple, just like this:
L1 = list()
L2 = []
L3 = [3,4,1,6,7,5]
L4 = [[2, 9, -5], [-1, 0, 4], [3, 1, 2]]
Here we create four lists: L1
, L2
, L3
and L4
. The first two lists are empty – they have zero elements. The two subsequent lists have some predefined values in them. L3
is a one-dimensional list, same as the one in the first image. L4
is a two-dimensional list, the same as in the second image. As you can see, L4
actually consists of three smaller 1D lists.
Whenever I mention an array, I usually mean a "NumPy" array. NumPy is a Python library (a library is a collection of pre-coded programs that allows you to perform many actions without writing your code from scratch), widely used for list/array operations. You can think of a NumPy array as a special kind of list, with lots of additional functions.
To create a NumPy array, you have to specify a size and use an initialization method. Here's an example:
import numpy as np
nparray = np.zeros((5,5))
In the first line, we import the NumPy library (as you can see, to import a library, you need to write import
) and by using as,
we give NumPy the abbreviation np
to make it easier to use. Then, we create a new array that we call nparray
, which is a 2D array of size 5 x 5, full of zeros. The initialization method is the part after the .
; in this case, we initialize this array as full of zeros, by using the function zeros
.
In order to get access to the values in a list or array, you need to give the index of this value. For example, if you wanted to change the first element in the L3
list, you would have to get its index. In Python, indexes start at 0
, so you would need to write L3[0]
. In fact, you can write print(L3[0])
and execute it, and you will see that, as you might hope, the number 3
will be displayed.
Accessing a single value in a multi-dimensional list/array requires you to input as many indexes as there are dimensions. For example, to get 0
from our L4
list, we would have to write L4[1][1]
. L4[1]
would return the entire second row, which is a list.
Exercise
Try to find the mean of all the numbers in the L4
list. There are multiple solutions.
Hint: The simplest solution makes use of the NumPy library. Check out some of its functions here: https://docs.scipy.org/doc/numpy/reference/
The solution is provided in the Chapter 03/Lists and Arrays/homework.py
file on the GitHub page.