Programmation fonctionnelle =========================== list comprehension ------------------ :: >>> l = [1,2,3,4] >>> my_list = [i**2 for i in l] >>> my_list [1, 4, 9, 16] >>> my_list = [ "number_%s" % i for i in l] >>> my_list ['number_1', 'number_2', 'number_3', 'number_4'] dict comprehension (a/c Py2.7) ------------------------------ :: >>> name = 'john doe' >>> { k: v for v, k in enumerate(name)} {' ': 4, 'e': 7, 'd': 5, 'h': 2, 'j': 0, 'o': 6, 'n': 3} map --- appliquer, en une seule instruction, une fonction donnée à tous les éléments d'une liste. :: >>> def add_5(n): ... return n + 5 >>> l = [1, 5, 9, 7] >>> list(map(add_5, l)) [6, 10, 14, 12] filter ------ :: >>> def contains_a(s): ... if 'a' in s: ... return True ... return False >>> l =['joe', 'bill', 'patricia', 'pamela', 'eric', 'aragorn'] >>> filter(contains_a, l) ['patricia', 'pamela', 'aragorn'] zip --- :: >>> x = ['a', 'b', 'c'] >>> y = [1, 2, 3] >>> zip(x, y) [('a', 1), ('b', 2), ('c', 3)]