Last active
March 9, 2024 14:26
-
-
Save TGITS/8c4737dbb42ce160a39e2859b3e5bf8c to your computer and use it in GitHub Desktop.
Exemples d'utilisation de la fonction map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("L'équivalent avec une boucle for : ", end="") | |
powers_of_2 = [] | |
for number in range(1, 10): | |
powers_of_2.append(number**2) | |
print(powers_of_2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
def squared(x): | |
return x**2 | |
print("Itérateurs créés à partir d'une map") | |
powers_of_2 = map(squared, range(1, 10)) | |
print( | |
"La fonction map retourne un objet iterator :", | |
powers_of_2, | |
) | |
print("C'est bien un objet iterator avec les méthodes __iter__ et __next__") | |
print( | |
"Il possède la fonction __iter__ ('__iter__' in dir(powers_of_2)) :", | |
"__iter__" in dir(powers_of_2), | |
) | |
print( | |
"Il possède la fonction __next__ ('__next__' in dir(powers_of_2)) :", | |
"__next__" in dir(powers_of_2), | |
) | |
print("Premier élément :", next(powers_of_2)) | |
print("Elément suivant :", next(powers_of_2)) | |
print("La suite des éléments : ", end="") | |
print(*powers_of_2) | |
print( | |
"Somme des carrés des 9 premiers entiers strictement positifs :", | |
sum(map(squared, range(1, 10))), | |
) | |
print( | |
"Maximum des carrés des 9 premiers entiers strictement positifs :", | |
max(map(squared, range(1, 10))), | |
) | |
print( | |
"Minimum des carrés des 9 premiers entiers strictement positifs :", | |
min(map(squared, range(1, 10))), | |
) | |
print("\n##############\n") | |
print("L'équivalent avec une boucle for : ", end="") | |
powers_of_2 = [] | |
for number in range(1, 10): | |
powers_of_2.append(squared(number)) | |
print(powers_of_2) | |
print("\n##############\n") | |
print("Liste avec map") | |
powers_of_2 = map(squared, range(1, 10)) | |
print( | |
"Liste des carrés des 9 premiers entiers strictement positifs :", | |
list(powers_of_2), | |
) | |
print("\n##############\n") | |
print("Ensemble avec map") | |
powers_of_2 = map(squared, range(-9, 10)) | |
print("Ensemble des carrés des entiers entre -9 et 9 :", set(powers_of_2)) | |
print("\n##############\n") | |
def squared_pair(x): | |
return (x, x**2) | |
print("Dictionnaire avec map") | |
iterator_powers_of_2_for_first_numbers = map(squared_pair, range(1, 10)) | |
print( | |
"Tableau associatif des carrés des 9 premiers entiers strictement positifs :", | |
dict(iterator_powers_of_2_for_first_numbers), | |
) | |
print("\n##############\n") | |
print("Compréhension équivalente à l'utilisation de map") | |
powers_of_2 = (x**2 for x in range(1, 10)) | |
print(powers_of_2) | |
print(next(powers_of_2)) | |
print(next(powers_of_2)) | |
print(*powers_of_2) | |
print("\n##############\n") | |
print("Itérateurs infinis à partir d'une map") | |
powers_of_2 = map(squared, itertools.count(1)) | |
print("Premier élément :", next(powers_of_2)) | |
print("Elément suivant :", next(powers_of_2)) | |
print("La suite des éléments pour les entiers inférieurs à 1000 : ", end="") | |
print(*itertools.takewhile(lambda x: x < 1000, powers_of_2)) | |
print("Elément suivant :", next(powers_of_2)) | |
print("\n##############\n") | |
print("Map et filter travaillant de concert") | |
first_event_numbers = filter(lambda x: x % 2 == 0, range(1, 10)) | |
print("Un objet _filter_, first_event_numbers :", first_event_numbers) | |
list_powers_of_2_for_first_event_numbers = map(squared, first_event_numbers) | |
print("Les premiers carrés :", list(list_powers_of_2_for_first_event_numbers)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def my_map(transformation_function, sequence): | |
for elt in sequence: | |
yield transformation_function(elt) | |
powers_of_two = my_map(lambda x: x**2, range(1, 11)) | |
print(powers_of_two) | |
print(next(powers_of_two)) | |
print(list(powers_of_two)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
powers_of_2 = map(lambda x: x**2, range(1, 10)) | |
print("La fonction map retourne un objet iterator :", powers_of_2) | |
print("C'est bien un objet iterator avec les méthodes __iter__ et __next__") | |
print( | |
"Il possède la fonction __iter__ ('__iter__' in dir(powers_of_2)) :", | |
"__iter__" in dir(powers_of_2), | |
) | |
print( | |
"Il possède la fonction __next__ ('__next__' in dir(powers_of_2)) :", | |
"__next__" in dir(powers_of_2), | |
) | |
print("Premier élément :", next(powers_of_2)) | |
print("Elément suivant :", next(powers_of_2)) | |
print("La suite des éléments : ", end="") | |
print(*powers_of_2) | |
print( | |
"Somme des carrés des 9 premiers entiers strictement positifs :", | |
sum(map(lambda x: x**2, range(1, 10))), | |
) | |
print( | |
"Maximum des carrés des 9 premiers entiers strictement positifs :", | |
max(map(lambda x: x**2, range(1, 10))), | |
) | |
print( | |
"Minimum des carrés des 9 premiers entiers strictement positifs :", | |
min(map(lambda x: x**2, range(1, 10))), | |
) | |
print(list(map(lambda x: x**2, range(1, 10)))) | |
# Ensemble des carrés des entiers entre -9 et 9 | |
print(set(map(lambda x: x**2, range(-9, 10)))) | |
def squared_pair(x): | |
return (x, x**2) | |
# Tableau associatif des carrés des 9 premiers entiers strictement positifs | |
print(dict(map(lambda x: (x, x**2), range(1, 10)))) | |
powers_of_2 = map(lambda x: x**2, itertools.count(1)) | |
next(powers_of_2) | |
next(powers_of_2) | |
print(*itertools.takewhile(lambda x: x < 1000, powers_of_2)) | |
next(powers_of_2) | |
first_event_numbers = filter(lambda x: x % 2 == 0, range(1, 10)) | |
list(map(lambda x: x**2, first_event_numbers)) | |
list(map(lambda x: x**2, filter(lambda x: x % 2 == 0, range(1, 10)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import starmap, repeat, count, islice | |
# La fonction map peut prendre plusieurs itérables en paramètres | |
# Il faut juste que la fonction appliquée par map, prennent en paramètre le même nombre d'arguments | |
print( | |
"[map] :", | |
*map(lambda x, y, z: x + y + z, ["1", "2", "3"], ["A", "B", "C"], ["a", "b", "c"]), | |
) | |
# Equivalent avec une compréhension | |
print( | |
"[compréhension] :", | |
*[x + y + z for x, y, z in zip(["1", "2", "3"], ["A", "B", "C"], ["a", "b", "c"])], | |
) | |
# Equivalent avec starmap (sans zip) | |
print( | |
"[starmap][avec zip] :", | |
*starmap( | |
lambda x, y, z: x + y + z, | |
zip(["1", "2", "3"], ["A", "B", "C"], ["a", "b", "c"]), | |
), | |
) | |
# Equivalent avec starmap (sans zip) | |
print( | |
"[starmap][sans zip] :", | |
*starmap( | |
lambda x, y, z: x + y + z, [("1", "A", "a"), ("2", "B", "b"), ("3", "C", "c")] | |
), | |
) | |
# Autre exemple avec la fonction puissance, pour calculer pour les nombres de 1 à 9, | |
# ce nombre elevé à la puissance de lui-même | |
print( | |
"[map] x puissance x, pour x entier de 1 à 9 : ", | |
*map(pow, range(1, 10), range(1, 10)), | |
) | |
# Equivalent avec starmap | |
print( | |
"[starmap] x puissance x, pour x entier de 1 à 9 : ", | |
*starmap(pow, zip(range(1, 10), range(1, 10))), | |
) | |
# Cela fonctionne aussi si l'un des iterables est infini, cela n'ira pas plus loin que le plus court des itérables | |
# la fonction repeat de itertools répète la valeur passée en paramètre _indéfiniment_ | |
print( | |
"[map] les 9 preniers chiffres à la puissance 2 : ", | |
*map(pow, range(1, 10), repeat(2)), | |
) | |
# Equivalent avec starmap | |
print( | |
"[starmap] les 9 preniers chiffres à la puissance 2 : ", | |
*starmap(pow, zip(range(1, 10), repeat(2))), | |
) | |
# Cela fonctionnera aussi si les 2 iterables sont infinis, il faudra juste faire attention en exploitant le retour de map | |
# La fonction count de itertools retourne une liste de valeurs partant d'une valeur de départ et d'un incrément (qui vaut 1 par défaut) | |
powers_of_2 = map(pow, count(1), repeat(2)) | |
print( | |
"[map] Les puissances de 2 pour les 20 premiers nombres à partir de 1 : ", | |
*islice(powers_of_2, 0, 20), | |
) | |
# Equivalent avec starmap | |
powers_of_2 = starmap(pow, zip(count(1), repeat(2))) | |
print( | |
"[map] Les puissances de 2 pour les 20 premiers nombres à partir de 1 : ", | |
*islice(powers_of_2, 0, 20), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment