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 | |
print('Compréhension de liste') | |
list_of_powers_of_2_for_first_numbers = [x**2 for x in range(1, 10)] | |
print(list_of_powers_of_2_for_first_numbers) | |
print('\n##############\n') | |
print("Compréhension d'ensemble") | |
set_of_powers_of_2_for_first_numbers = {x**2 for x in 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
import itertools | |
import functools | |
import operator | |
columns = ("a", "b", "c", "d", "e", "f", "g", "h") | |
rows = (1, 2, 3, 4, 5, 6, 7, 8) | |
# Approche naïve | |
print( | |
"Approche naïve (1) : ", list(map(lambda x: map(lambda y: (x, y), rows), columns)) |
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 functools | |
def my_filter(predicate, seq): | |
def filter_and_aggregate_as_list(accumulator, current_elt): | |
if predicate(current_elt): | |
accumulator.append(current_elt) | |
return accumulator | |
return functools.reduce(filter_and_aggregate_as_list, seq, []) |
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 | |
houses = ["tyrell", "stark", "lannister", "tarly", "baratheon", "targaryen"] | |
print("Houses :", houses) | |
print("\n##############\n") | |
houses_starting_with_t = filter(lambda s: s.startswith("t"), houses) | |
print("La fonction filter retourne un objet iterator :", houses_starting_with_t) | |
print("C'est bien un objet iterator avec les méthodes __iter__ et __next__") |
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
houses = ["Stark", "Lannister", "Baratheon", "Greyjoy"] | |
seats = ["Winterfell", "Casterly Rock", "Storm's End", "Pyke"] | |
sigils = ["A Gray Direwolf", "A Golden Lion", "A Crowned Black Stag", "A Golden Kraken"] | |
words = ["Winter is coming", "Hear me roar !", "Our is the fury !", "We do not sow"] | |
print(f"houses : {houses}") | |
print(f"seats : {seats}") | |
print(f"sigils : {sigils}") | |
print(f"words : {words}") |
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 groupby | |
from collections import Counter | |
def frequencies_map_with_for_from(elements): | |
frequencies_map = {} | |
for element in elements: | |
if element in frequencies_map.keys(): | |
frequencies_map[element] = frequencies_map[element] + 1 | |
else: |
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
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] | |
print('letters {}'.format(letters)) | |
print('\n##########\n') | |
print('In Python, the for is a foreach : ') | |
for letter in letters: | |
print(letter) |
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
bonjour = "Hello World" | |
print(bonjour) | |
for c in reversed(bonjour): | |
print(c, end="") | |
print() | |
print([c for c in reversed(bonjour)]) |
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
tuple = (1, 2, 3, 4) | |
for i in reversed(tuple): | |
print(str(i) + " ", end="") |
NewerOlder