Commit 8ecbfed9 authored by Franck Thollard's avatar Franck Thollard
Browse files

adding zip and a diy on computation of the euclidian distance

parent ad24a49c
Pipeline #86266 passed with stage
in 1 minute and 2 seconds
......@@ -1058,10 +1058,28 @@
cities = ["grenoble", "paris", "berlin", "london"]
for city in cities:
print(city, end=", ")
```
%% Cell type:markdown id: tags:
### Print elements of a list and its indexes
%% Cell type:code id: tags:
``` python
index = 0
cities = ["grenoble", "paris", "berlin", "london"]
for city in cities:
print(f"({index}, {city})")
index = index+1
```
%% Cell type:markdown id: tags:
### This is a classical snippet, hence the built-in function enumerate
%% Cell type:code id: tags:
``` python
# the built-in function enumerate is very useful
for index, city in enumerate(cities):
......@@ -1079,17 +1097,78 @@
['Grenoble', 'Paris', 'Berlin', 'London']
%% Cell type:markdown id: tags:
### Iterate synchoneously in many lists
The two following piece of code are equivalent
%% Cell type:code id: tags:
``` python
cities = ["grenoble", "paris", "berlin", "london"]
areas = [18.1, 105.4, 891.7, 1500.]
populations = [158198, 2165423, 3748148, 14372596]
for index, city in enumerate(cities):
print(f"density({city}) = {populations[index]/areas[index]}")
```
%% Cell type:code id: tags:
``` python
cities = ["grenoble", "paris", "berlin", "london"]
areas = [18.1, 105.4, 891.7, 1500.]
populations = [158198, 2165423, 3748148, 14372596]
for city, area, population in zip(cities, areas, populations):
print(f"density({city}) = {population/area:.2f}")
```
%% Cell type:markdown id: tags:
### DIY Compute square of the L2 distance between two vectors
V1 = [ 1, 6, 9, 10, 4]
and
V2 = [ 0, 4, 1, 9, 6]
%% Cell type:markdown id: tags:
### A possible solution:
%% Cell type:code id: tags:
``` python
V1 = [ 1, 6, 9, 10, 4]
V2 = [ 0, 4, 1, 9, 6]
sum_square_dists = 0
for v1, v2 in zip(V1, V2):
sum_square_dists += (v1-v2)*(v1-v2)
dist = math.sqrt(sum_vect)
print(f"sum square dists = {dist}")
```
%% Cell type:markdown id: tags:
### Loops: keywords `continue` and `break`
- `continue`: passes the block in the loop and continues the loop.
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
for x in range(1, 8):
if x == 5:
continue
print(x, end=', ')
```
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment