Utilisation des itérateurs

Boîte à outils Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Utilisation de enumerate()

avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']
e = enumerate(avengers)

print(type(e))
<class 'enumerate'>
e_list = list(e)

print(e_list)
[(0, 'hawkeye'), (1, 'iron man'), (2, 'thor'), (3, 'quicksilver')]
Boîte à outils Python

enumerate() et unpack

avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']

for index, value in enumerate(avengers): print(index, value)
0 hawkeye
1 iron man
2 thor
3 quicksilver
for index, value in enumerate(avengers, start=10):
    print(index, value)
10 hawkeye
11 iron man
12 thor
13 quicksilver
Boîte à outils Python

Utilisation de zip()

avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']
names = ['barton', 'stark', 'odinson', 'maximoff']

z = zip(avengers, names)
print(type(z))
<class 'zip'>
z_list = list(z)

print(z_list)
[('hawkeye', 'barton'), ('iron man', 'stark'), 
('thor', 'odinson'), ('quicksilver', 'maximoff')]
Boîte à outils Python

zip() et unpack

avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']
names = ['barton', 'stark', 'odinson', 'maximoff']

for z1, z2 in zip(avengers, names): print(z1, z2)
hawkeye barton
iron man stark
thor odinson
quicksilver maximoff
Boîte à outils Python

Afficher le fichier zip avec *

avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver']
names = ['barton', 'stark', 'odinson', 'maximoff']
z = zip(avengers, names)
print(*z)
('hawkeye', 'barton') ('iron man', 'stark') 
('thor', 'odinson') ('quicksilver', 'maximoff')
Boîte à outils Python

Passons à la pratique !

Boîte à outils Python

Preparing Video For Download...