how to train your python: iterables (fr)

22
Jordi Riera Software developer @ Rodeo FX Founder @ cgstudiomap.org 8 ans à écrire du python [email protected] @jordiriera_cg https://www.linkedin.com/in/jo rdirieracg/ https://github.com/foutoucour/

Upload: jordi-riera

Post on 23-Feb-2017

102 views

Category:

Software


0 download

TRANSCRIPT

Page 1: How to train your python: iterables (FR)

Jordi Riera● Software developer @ Rodeo FX● Founder @ cgstudiomap.org● 8 ans à écrire du python

[email protected]● @jordiriera_cg● https://www.linkedin.com/in/jordirieracg/● https://github.com/foutoucour/

Page 2: How to train your python: iterables (FR)

How To Train Your PythonLes bases sur les iterables

Page 3: How to train your python: iterables (FR)

Iterables+ de 10 types d’iterables

Page 4: How to train your python: iterables (FR)

liste, et pas que● List & deque● Tuple & namedtuple● String● Set & frozenset● Dict, Ordereddict, ChainMap, Counter & defaultdict● Generators● Range, zip, map, file object, et autres.

Page 5: How to train your python: iterables (FR)

Lequel choisir?

Page 6: How to train your python: iterables (FR)

Lequel choisir?● List, deque, tuple, string, generators, Ordereddict: ordonnés● Tuple, frozenset: immuables (well... kind of... :( )● Set & frozenset: caractère unique● Dict, Ordereddict, ChainMap, Counter & defaultdict: mapping● String: ... ben... string quoi...● Generators, range, zip, map, etc: optimisation, consommation

Page 7: How to train your python: iterables (FR)

for looplanguages = [‘php’, ‘ruby’, ‘python’]frameworks = [‘symfony’, ‘ruby on rails’, ‘django’]

for i in range(len(languages)): print(languages[i] + ‘: ’ + frameworks[i])

Page 8: How to train your python: iterables (FR)

for looplanguages = [‘php’, ‘ruby’, ‘python’]frameworks = [‘symfony’, ‘ruby on rails’, ‘django’]

for i, language in enumerate(languages): print(‘: ’.join([language, frameworks[i]]))

for language, framework in zip(languages, frameworks): print(‘: ’.join([language, framework]))

Page 9: How to train your python: iterables (FR)

Setrandom_numbers = [ 3, 4, 4, 1, 2, 3, 1]

set(random_numbers)>>> {1, 2, 3, 4}

frozenset(random_numbers)>>> frozenset({1, 2, 3, 4})

Page 10: How to train your python: iterables (FR)

SetSets acceptent les opérations mathématiques:● Union● Intersection● Difference● Et d’autres opérations

plus chelou, mais ça fait de jolies figures

Page 11: How to train your python: iterables (FR)

dict, feel the powa!

Page 12: How to train your python: iterables (FR)

mapping = dict() # ou {}for language, framework in zip(languages, frameworks): if not language in languages: mapping[language] = []

mapping[language].append(framework)

dict

Page 13: How to train your python: iterables (FR)

mapping = defaultdict(list)for language, framework in zip(languages, frameworks): mapping[language].append(framework)

mapping = {}for l, framework in zip(languages, frameworks): mapping.setdefault(l, []).append(framework)

dict

Page 14: How to train your python: iterables (FR)

dictmapping = {‘php’:[‘symfony’], ‘ruby’:[’ruby on rails’], ‘python’:[’django’]}

for language in mapping: print(language)

for language, frameworks in mapping.items(): print(language, frameworks)

Page 15: How to train your python: iterables (FR)

dictmapping = {‘php’:[‘symfony’], ‘ruby’:[’ruby on rails’], ‘python’:[’django’]}

print(mapping[‘python’])del mapping[‘python’] print(mapping.get(‘python’, ‘flask’))Mapping[‘python’] = ‘pyramid’

Page 16: How to train your python: iterables (FR)

muabilité# Les listes sont muables. Elles peuvent être mis à jour:

list1 = [1,]list1.append(2)list1 == [1, 2]

Et d’autres méthodes...

list2 = [1,]list2.insert(0, 2)list2 == [2, 1]

Page 17: How to train your python: iterables (FR)

muabilité# Cool mais...

list2 = list1list1.append(3)list2 == [1, 2, 3]

# list2 “pointe” vers list1. # list1 et list2 sont la même liste en fait...

Page 18: How to train your python: iterables (FR)

muabilité# Solutionlist2 = list1[:]

# list2 est une liste avec tous les éléments de list1

# you’re welcome ;)

Page 19: How to train your python: iterables (FR)

# Les tuples sont immuables, # ils ne peuvent pas être mis à jour:tuple1 = (1,)tuple1.append(2)Raise AttributeError

immuabilité

Page 20: How to train your python: iterables (FR)

# enfin...list1 = [1,]tuple1 = (1, list1) list1.append(2)tuple1 == (1, [1, 2])

# pas cool bro!

immuabilité

Page 21: How to train your python: iterables (FR)

Compréhension à la portée de tous!

list1 = [x for x in z if not x == ‘foo’]

gen = (x for x in z if not x == ‘foo’)# Nope c’est pas un tuple! Mais un générateur.

set1 = {x for x in z if not x == ‘foo’}

dic1 = {x: x.bar for x in z if not x == ‘foo’}

Page 22: How to train your python: iterables (FR)

Questions?

[email protected]● @jordiriera_cg● https://www.linkedin.com/in/jordirieracg/● https://github.com/foutoucour/