Skip to content

Instantly share code, notes, and snippets.

@TGITS
Last active April 17, 2024 05:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TGITS/11869409350df078e2eca5ede7f5ff5d to your computer and use it in GitHub Desktop.
Save TGITS/11869409350df078e2eca5ede7f5ff5d to your computer and use it in GitHub Desktop.
Différents exemples de compréhensions en Python
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)}
print(set_of_powers_of_2_for_first_numbers)
print('\n##############\n')
print("Compréhension de dictionnaire")
dict_powers_of_2_for_first_numbers = {x: x**2 for x in range(1, 10)}
print(dict_powers_of_2_for_first_numbers)
print('\n##############\n')
print("Générateurs créés à partir d'une compréhension")
generator_of_powers_of_2_for_first_numbers = (x**2 for x in range(1, 10))
print(generator_of_powers_of_2_for_first_numbers)
print(next(generator_of_powers_of_2_for_first_numbers))
print(next(generator_of_powers_of_2_for_first_numbers))
print(*generator_of_powers_of_2_for_first_numbers)
print(sum((x**2 for x in range(1, 10))))
print(max((x**2 for x in range(1, 10))))
print(min((x**2 for x in range(1, 10))))
print('\n##############\n')
print("Générateurs _infinis_ à partir d'une compréhension")
neverending_generator_of_powers_of_2 = (x**2 for x in itertools.count(1))
print(next(neverending_generator_of_powers_of_2))
print(next(neverending_generator_of_powers_of_2))
print(*itertools.takewhile(lambda x: x < 1000,
neverending_generator_of_powers_of_2))
print(next(neverending_generator_of_powers_of_2))
print('\n##############\n')
print('Compréhension de liste avec un filtrage')
list_powers_of_2_for_first_event_numbers = [
x**2 for x in range(1, 10) if x % 2 == 0]
print(list_powers_of_2_for_first_event_numbers)
print('\n##############\n')
print('Compréhension imbriquée')
columns = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
rows = (1, 2, 3, 4, 5, 6, 7, 8)
chessboard = [(c, r) for c in columns for r in rows]
print(chessboard)
print('\n##############\n')
print('Compréhension imbriquée avec filtrage')
sum_of_even_and_odd = [
x + y for x in range(10) if x % 2 == 0 for y in range(10) if y % 2 == 1]
print(sum_of_even_and_odd)
columns = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
rows = (1, 2, 3, 4, 5, 6, 7, 8)
chessboard = [(c, r) for c in columns for r in rows]
print(chessboard)
print()
chessboard_with_for = []
for c in columns:
for r in rows:
chessboard_with_for.append((c, r))
print(chessboard_with_for)
print()
computations = [x*y for x in range(1, 10) if x % 2 == 1 for y in range(10) if y % 2 == 1]
print(computations)
lower_case_houses = ["tyrell", "stark",
"lannister", "tarly", "baratheon", "targaryen"]
print('Houses in lower case :', lower_case_houses)
capitalized_houses = [(s, s.capitalize()) for s in lower_case_houses]
print(capitalized_houses)
capitalize_houses_starting_by_t = [
s.capitalize() for s in lower_case_houses if s[0] == 't']
print(capitalize_houses_starting_by_t)
fornames = ["Samwell", "Jon", "Brandon", "Tyrion",
"Cersei", "Daenerys", "Sansa", "Arya"]
names = ["Snow", "Stark", "Lannister", "Tarly", "Targaryen"]
got_names = ["{} {}".format(forname,name) for forname in fornames for name in names]
print(got_names)
import itertools
generator = (x**2 for x in itertools.count(5))
print(next(generator))
print(next(generator))
for i in range(10):
next(generator)
print(next(generator))
for i in range(10):
next(generator)
print(next(generator))
houses = ["Tyrell", "Stark", "Lannister", "Tarly", "Baratheon", "Targaryen"]
every_houses_except_the_lannister = [s for s in houses if not s.startswith("L")]
print(every_houses_except_the_lannister)
houses = ["Tyrell", "Stark", "Lannister", "Tarly", "Baratheon", "Targaryen"]
fornames = ["Jon", "Eddard", "Arya", "Sansa", "Jaime", "Samwell"]
noble_full_names = [forname + " " + house for house in houses for forname in fornames]
print(noble_full_names)
full_names = [forname + " " + house for house in houses for forname in fornames if not house.startswith("T")]
print(full_names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment