ARRAYS
Contents
ARRAYS#
An array is a data structure, which can store a collection of elements.
So, we are talking about list
?
Not exactly. Although there are similarities between list and array, in Python they are different structures.
Python List |
Array |
---|---|
Built-in function |
External function (e.g. NumPy) |
Multiple types |
Single type |
Flexible size |
Fixed-size* |
Slow* |
Fast* |
Flexible |
Computational computing |
Creating a NumPy array#
Note: We are going to use NumPy arrays, but there are other external packages to use.
Bellow see different ways of creating a NumPy Array
import numpy as np
From list#
a = np.array([1, 2, 3])
a
array([1, 2, 3])
Array of zeros#
b = np.zeros((2,4))
b
array([[0., 0., 0., 0.],
[0., 0., 0., 0.]])
Array of ones (same shape of b
)#
c = np.ones_like(b, dtype=int) # specify type!
c
array([[1, 1, 1, 1],
[1, 1, 1, 1]])
Array of sequence#
d = np.arange(0, 12, 2)
d
array([ 0, 2, 4, 6, 8, 10])
Reshaping#
e = d.reshape((2, 3))
e
array([[ 0, 2, 4],
[ 6, 8, 10]])
List versus Array#
Element types#
List
support different element types. Bellow you can se a list with integer
and real numbers.
list1 = [1, 2, 3, 4.0]
list1
[1, 2, 3, 4.0]
NumPy array just support one type of element. So, in order to convert the above
list
in an array
, all elements are converted to real numbers:
array1 = np.array(list1)
array1
array([1., 2., 3., 4.])
Mathematical operation#
Lets suppose that we want multiply all items in a list by two, so we do:
list2 = [1, 2, 3, 4, 5]
list2*2
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
This was not what we were expecting. Instead multiply every element by two, Python doubles the list.
To solve this problem, the solution would be to use a loop:
list3 = []
for item in list2:
list3.append(item*2)
list3
[2, 4, 6, 8, 10]
list3 = [item*2 for item in list2]
list3
[2, 4, 6, 8, 10]
Now, lets see how to do this using NumPy array:
array2 = np.array([1, 2, 3, 4, 5])
array3 = array2*2
array3
array([ 2, 4, 6, 8, 10])
Speed#
Using NumPy array for mathematical operations is not just simpler, it is also (usually) faster.
from time import time
list4 = list(range(100000))
array4 = np.array(list4)
Using List & Loop#
t0 = time()
list5 = []
for item in list4:
list5.append(item*2)
print("Execution time: ", time()-t0)
list5[0:10]
Execution time: 0.015093326568603516
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Using List Comprehension#
t0 = time()
list5 = [item*2 for item in list4]
print("Execution time: ", time()-t0)
list5[0:10]
Execution time: 0.007974624633789062
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Using NumPy Array#
t0 = time()
array5 = array4*2
print("Execution time: ", time()-t0)
array5[0:10]
Execution time: 0.00042819976806640625
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
Indexation#
Indexation is related with access a element by referring to its index number.
We are going to use a 2D-list/array to this example:
list6 = [[1, 2, 3],
[4, 5, 6]]
array6 = np.array(list6)
In both cases, 6
is the element in the “line” 1
, “column” 2
: so
index 1, 2
.
The way of using indexes are different in lists and arrays, lets see some scenarios.
Print an element#
Lets print one single element: the element 6
(index 1, 2
):
List#
list6[1][2]
6
Array#
array6[1][2]
6
The above works, but is more common to see the bellow syntax:
array6[1, 2]
6
Print Line#
Now, lets print an entire line, the line 0
:
List#
list6[0][:]
[1, 2, 3]
or
list6[0]
[1, 2, 3]
Array#
array6[0][:]
array([1, 2, 3])
or
array6[0]
array([1, 2, 3])
using an array like syntax:
array6[0, :]
array([1, 2, 3])
Print Column#
Finally, lets print an entire column, the column 1
:
List#
list6[:][1]
[4, 5, 6]
Is not what we was expecting!
To print a list column we need to use a for
loop:
print([line[1] for line in list6])
[2, 5]
Array#
array6[:][1]
array([4, 5, 6])
Wrong again!
But for array there is a simpler solution!
array6[:, 1]
array([2, 5])
Advanced Indexation#
Suppose that every 3 elements in a list/array, you need to decrease the element value by 1, that is, you want to transform:
10, 20, 30, 40, 50, 60
in
10, 20, 29, 40, 50, 59
First, we can create a variable to store all the indexes that we want to transform,
in this case is 2
and 5
.
index = range(2, 6, 3)
print(list(index))
[2, 5]
Define list and array:
list7 = [10, 20, 30, 40, 50, 60]
array7 = np.array(list7)
The Advanced Array Indexation!#
Using array gives you the option to perform operations on a list of indexes rather than a specific index.
array7[index] -= 1
array7
array([10, 20, 29, 40, 50, 59])
list7[index] -= 1
list7
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_1897/2434500378.py in <module>
----> 1 list7[index] -= 1
2 list7
TypeError: list indices must be integers or slices, not range