Recall the mathematical notation:
$$L_1 = \left\{x^2 : x \in \{0,1,\ldots ,9\}\right\}$$$$L_2 = \left(1, 2, 4, 8,\ldots, 2^{12}\right)$$[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
L1 = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] L2 = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]
List comprehension with conditions
$$M = \left\{x \mid x \in L_1 \text{ if } x \text{ is even}\right\}$$L3 = [0, 4, 16, 36, 64]
Nested use of link comprehension
[0, 4, 16, 36, 64]
Use of list comprehension for string processing
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
['THE', 'QUICK', 'BROWN', 'FOX', 'JUMPS', 'OVER', 'THE', 'LAZY', 'DOG']
[['THE', 'the'], ['QUICK', 'quick'], ['BROWN', 'brown'], ['FOX', 'fox'], ['JUMPS', 'jumps'], ['OVER', 'over'], ['THE', 'the'], ['LAZY', 'lazy'], ['DOG', 'dog']]
['quick', 'brown', 'jumps', 'over', 'lazy']
Use list comprehension for obtaining input
Give numbers separated by comma: 1,2,3,7 [1, 2, 3, 7]
Creating vectors and matrices
Create a vector of 10 zeros
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Set the diagonal to 1
[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]]
Create a list of 10 random integers in [0,99]
[63, 65, 41, 9, 4, 3, 54, 79, 41, 90]
Removing elements from a list
Removing elements from a list while you iterate through it can lead to problems...
[1, 4, 5, 8]
Another way to do this using list comprehension:
We can create a new list with the entries we want to keep
Or create a list with the entries we want to remove
[1, 5]
Using a dictionary in the list comprehension
['B', 'C']
We can create dictionaries in a similar way as with list comprehension
{'1': 1, '2': 2, '3': 3, '4': 4, '5': 5}
{'apple': 5, 'mango': 5, 'banana': 6, 'cherry': 6}
{'Apple': 0, 'Mango': 1, 'Banana': 2, 'Cherry': 3}
{0: 'Apple', 1: 'Mango', 2: 'Banana', 3: 'Cherry'}
{0: 'Apple', 1: 'Mango', 2: 'Banana', 3: 'Cherry'}
Using the right data structure makes a big difference when handling large data. Dictionaries and sets have expected constant time for finding an element, while lists have linear complexity. Even logarithmic time is significantly faster than linear.
Example
Looking for 100K integers in a collection of 100K integers
0.020679712295532227
111.83691191673279
Example
You are given a graph in the form of a collection of edges, that is, pairs of vertices
How do you store it in order to be able to quickly answer if there is an edge (x,y) in the graph, and also to get all the neighbors of node?
Create a dictionary with nodes as the keys, and sets with neighboring nodes as values
{1: {2}, 2: {1, 3, 5, 6, 7}, 3: {2, 4, 5}, 5: {2, 3, 6, 7}, 6: {2, 5}, 7: {2, 5, 8}, 4: {3}, 8: {7, 9, 10}, 9: {8}, 10: {8}}
{1, 3, 5, 6, 7}
We will often need to work with randomness. A library for this that is part of the main Python distribution is the random library.
Useful functions:
How do I implement the following?
With probability 0.7 print 'A', with probability 0.2 print 'B', and with probability 0.1 do nothing.
A
From a list I want to sample k elements where k is a parameter. I want my samples for the different k's to have the property that smaller samples are subsets of bigger samples. That is, the sample of size k+1 will contain one more random element to the sample of size k.
[5, 4, 13, 2] [5, 4, 13, 2, 6]