Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tp-bigdata
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LOGIN-BIGDATA
tp-bigdata
Commits
087c039d
Commit
087c039d
authored
2 months ago
by
Helene Coullon
Browse files
Options
Downloads
Patches
Plain Diff
update tp python
parent
134f6446
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tp4-5-6/tp4/tp-python.ipynb
+78
-18
78 additions, 18 deletions
tp4-5-6/tp4/tp-python.ipynb
with
78 additions
and
18 deletions
tp4-5-6/tp4/tp-python.ipynb
+
78
−
18
View file @
087c039d
...
...
@@ -6,12 +6,12 @@
"source": [
"# Map reduce\n",
"\n",
"Le code disponible ci-dessous
est celui du cours. Bien entendu dans ce cas on est limités par la puissance et la mémoire de la machine
utilis
ée
."
"Le code disponible ci-dessous
donne des exemples d'
utilis
ation
."
]
},
{
"cell_type": "code",
"execution_count":
null
,
"execution_count":
2
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -27,17 +27,34 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"Exemples d'utilisation de Map en Python. Nous définissons aussi un `flatmap`.\n",
"\n",
"\n",
"# Define flat_map using map and itertools.chain\n",
"def flat_map(func, iterable):\n",
" return list(chain.from_iterable(map(func, iterable)))\n",
"\n",
"Exécutez et comprenez ces codes."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Lengths: [3, 4, 6, 5]\n",
"Test on list function: ['T', 'e', 's', 't']\n",
"List of characters: [['T', 'h', 'e'], ['D', 'a', 'r', 'k'], ['K', 'n', 'i', 'g', 'h', 't'], ['R', 'i', 's', 'e', 's']]\n",
"Test on ord function: 84\n",
"List of ASCII values: [84, 68, 75, 82]\n",
"Flattened characters: ['T', 'h', 'e', 'D', 'a', 'r', 'k', 'K', 'n', 'i', 'g', 'h', 't', 'R', 'i', 's', 'e', 's']\n",
"Incremented lengths: [4, 5, 7, 6]\n"
]
}
],
"source": [
"# Initial list of words\n",
"words = [\"The\", \"Dark\", \"Knight\", \"Rises\"]\n",
"\n",
...
...
@@ -45,15 +62,27 @@
"lengths = list(map(len, words))\n",
"print(\"Lengths:\", lengths)\n",
"\n",
"# The List function creates a list from any iterable object\n",
"print(\"Test on list function:\",list(\"Test\"))\n",
"\n",
"# Map to get each word as a list of characters\n",
"list_of_chars = list(map(list, words))\n",
"print(\"List of characters:\", list_of_chars)\n",
"\n",
"# The ord function returns the ASCII value of a character\n",
"print(\"Test on ord function:\",ord(\"T\"))\n",
"\n",
"# Map to get the ASCII value of the first character of each word\n",
"# This example uses a lambda anonymous function: https://www.w3schools.com/python/python_lambda.asp\n",
"list_of_asciis = list(map(lambda word: ord(word[0]), words))\n",
"print(\"List of ASCII values:\", list_of_asciis)\n",
"\n",
"# Define flat_map using map and itertools.chain\n",
"def flat_map(func, iterable):\n",
" return list(chain.from_iterable(map(func, iterable)))\n",
"\n",
"# FlatMap to flatten all characters\n",
"# compare the output to the list_of_chars = list(map(list, words))\n",
"chars = flat_map(list, words)\n",
"print(\"Flattened characters:\", chars)\n",
"\n",
...
...
@@ -69,11 +98,32 @@
"## Reduce"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exemples d'utilisation de Reduce en Python.\n",
"\n",
"Exécutez et comprenez ces codes."
]
},
{
"cell_type": "code",
"execution_count":
null
,
"execution_count":
11
,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"res1: TheDarkKnightRises\n",
"res2: TheDarkKnightRisesAndFalls\n",
"res3: NaNaTheDarkKnightRises\n",
"res4: TDKR\n",
"res5: 18\n"
]
}
],
"source": [
"words = [\"The\", \"Dark\", \"Knight\", \"Rises\"]\n",
"lengths = list(map(len, words)) # Creates a list of lengths: [3, 4, 6, 5]\n",
...
...
@@ -110,22 +160,32 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Ecrire un programme qui calcule la distance totale entre une série de points en 2D, connectés séquentiellement comme un trajet.\n",
"On vous donne une liste de points en 2D, chaque point étant représenté par un tuple (x, y).\n",
"Ecrire un programme qui calcule la distance Euclidienne totale entre une série de points en 2D connectés séquentiellement l'un après l'autre.\n",
"\n",
"
Utilisez map pour calculer la distance entre chaque paire de points consécutifs
.\n",
"
On vous donne une liste de points en 2D, chaque point étant représenté par un tuple (x, y)
.\n",
"\n",
"Utilisez ensuite reduce pour calculer la distance totale du trajet reliant tous les points.\n",
"- Utilisez map pour calculer la distance entre chaque paire de points consécutifs.\n",
"- Utilisez ensuite reduce pour calculer la distance totale du trajet reliant tous les points.\n",
"\n",
"```python\n",
"points = [(0, 0), (3, 4), (7, 1), (10, 10)]\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"from functools import reduce\n",
"import math"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3
(ipykernel)
",
"language": "python",
"name": "python3"
},
...
...
@@ -139,7 +199,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.
3
"
"version": "3.12.
9
"
}
},
"nbformat": 4,
...
...
%% Cell type:markdown id: tags:
# Map reduce
Le code disponible ci-dessous
est celui du cours. Bien entendu dans ce cas on est limités par la puissance et la mémoire de la machine
utilis
ée
.
Le code disponible ci-dessous
donne des exemples d'
utilis
ation
.
%% Cell type:code id: tags:
```
python
from
itertools
import
chain
from
functools
import
reduce
```
%% Cell type:markdown id: tags:
## Map
%% Cell type:
code
id: tags:
%% Cell type:
markdown
id: tags:
```
python
Exemples d'utilisation de Map en Python. Nous définissons aussi un
`flatmap`
.
Exécutez et comprenez ces codes.
# Define flat_map using map and itertools.chain
def
flat_map
(
func
,
iterable
):
return
list
(
chain
.
from_iterable
(
map
(
func
,
iterable
)))
%% Cell type:code id: tags:
```
python
# Initial list of words
words
=
[
"
The
"
,
"
Dark
"
,
"
Knight
"
,
"
Rises
"
]
# Map to get the length of each word
lengths
=
list
(
map
(
len
,
words
))
print
(
"
Lengths:
"
,
lengths
)
# The List function creates a list from any iterable object
print
(
"
Test on list function:
"
,
list
(
"
Test
"
))
# Map to get each word as a list of characters
list_of_chars
=
list
(
map
(
list
,
words
))
print
(
"
List of characters:
"
,
list_of_chars
)
# The ord function returns the ASCII value of a character
print
(
"
Test on ord function:
"
,
ord
(
"
T
"
))
# Map to get the ASCII value of the first character of each word
# This example uses a lambda anonymous function: https://www.w3schools.com/python/python_lambda.asp
list_of_asciis
=
list
(
map
(
lambda
word
:
ord
(
word
[
0
]),
words
))
print
(
"
List of ASCII values:
"
,
list_of_asciis
)
# Define flat_map using map and itertools.chain
def
flat_map
(
func
,
iterable
):
return
list
(
chain
.
from_iterable
(
map
(
func
,
iterable
)))
# FlatMap to flatten all characters
# compare the output to the list_of_chars = list(map(list, words))
chars
=
flat_map
(
list
,
words
)
print
(
"
Flattened characters:
"
,
chars
)
# Map to increment each word length by 1
incs
=
list
(
map
(
lambda
length
:
length
+
1
,
lengths
))
print
(
"
Incremented lengths:
"
,
incs
)
```
%% Output
Lengths: [3, 4, 6, 5]
Test on list function: ['T', 'e', 's', 't']
List of characters: [['T', 'h', 'e'], ['D', 'a', 'r', 'k'], ['K', 'n', 'i', 'g', 'h', 't'], ['R', 'i', 's', 'e', 's']]
Test on ord function: 84
List of ASCII values: [84, 68, 75, 82]
Flattened characters: ['T', 'h', 'e', 'D', 'a', 'r', 'k', 'K', 'n', 'i', 'g', 'h', 't', 'R', 'i', 's', 'e', 's']
Incremented lengths: [4, 5, 7, 6]
%% Cell type:markdown id: tags:
## Reduce
%% Cell type:markdown id: tags:
Exemples d'utilisation de Reduce en Python.
Exécutez et comprenez ces codes.
%% Cell type:code id: tags:
```
python
words
=
[
"
The
"
,
"
Dark
"
,
"
Knight
"
,
"
Rises
"
]
lengths
=
list
(
map
(
len
,
words
))
# Creates a list of lengths: [3, 4, 6, 5]
# Concatenates all elements in `words` into a single string.
res1
=
reduce
(
lambda
x
,
y
:
x
+
y
,
words
)
if
words
else
None
print
(
"
res1:
"
,
res1
)
# Concatenates all elements in `words`, then adds "AndFalls" at the end.
res2
=
reduce
(
lambda
x
,
y
:
x
+
y
,
words
+
[
"
AndFalls
"
])
print
(
"
res2:
"
,
res2
)
# Concatenates "NaNa" at the beginning, then adds all elements in `words`.
res3
=
reduce
(
lambda
x
,
y
:
x
+
y
,
[
"
NaNa
"
]
+
words
)
print
(
"
res3:
"
,
res3
)
# Takes the first letter of each word in `words` and concatenates them.
res4
=
reduce
(
lambda
x
,
y
:
x
+
y
,
map
(
lambda
word
:
word
[
0
],
words
))
print
(
"
res4:
"
,
res4
)
# Sums up all the elements in `lengths`, which represents the total length of all words.
res5
=
reduce
(
lambda
x
,
y
:
x
+
y
,
lengths
)
print
(
"
res5:
"
,
res5
)
```
%% Output
res1: TheDarkKnightRises
res2: TheDarkKnightRisesAndFalls
res3: NaNaTheDarkKnightRises
res4: TDKR
res5: 18
%% Cell type:markdown id: tags:
## Exercice
%% Cell type:markdown id: tags:
Ecrire un programme qui calcule la distance totale entre une série de points en 2D, connectés séquentiellement comme un trajet.
On vous donne une liste de points en 2D, chaque point étant représenté par un tuple (x, y).
Ecrire un programme qui calcule la distance Euclidienne totale entre une série de points en 2D connectés séquentiellement l'un après l'autre.
Utilisez map pour calculer la distance entre chaque paire de points consécutifs
.
On vous donne une liste de points en 2D, chaque point étant représenté par un tuple (x, y)
.
Utilisez ensuite reduce pour calculer la distance totale du trajet reliant tous les points.
-
Utilisez map pour calculer la distance entre chaque paire de points consécutifs.
-
Utilisez ensuite reduce pour calculer la distance totale du trajet reliant tous les points.
```
python
points
=
[(
0
,
0
),
(
3
,
4
),
(
7
,
1
),
(
10
,
10
)]
```
%% Cell type:code id: tags:
```
python
from
functools
import
reduce
import
math
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment