Tableaux/listes en Python (2/2) - Matrices, et sombres histoires de

Tableaux/listes en
Python (2/2)
Stéphane Gonnord
Plan
Tableaux et listes en
Python
Tableaux
bidimensionnels
Un peu de
numpy.array
Tableaux/listes en Python (2/2)
Matrices, et sombres histoires de pointeurs
Stéphane Gonnord
www.mp933.fr
Lycée du parc - Lyon
Vendredi 7 février 2014
Tableaux/listes en
Python (2/2)
Stéphane Gonnord
Plan
Tableaux et listes en
Python
Tableaux
bidimensionnels
Un peu de
numpy.array
Plan
1. Quelques rappels :
ICréation, indexation
ISlicing
ICopie
IPassage en paramètre
IParcours
2. Tableaux bidimensionnels :
ICréation basique, indexation
ICréation-bis : le drame
IRecopie
3. Un peu de numpy
ILes tableaux array
IMatrices
ISlicing multidimensionnel.
Tableaux/listes en
Python (2/2)
Stéphane Gonnord
Plan
Tableaux et listes en
Python
Tableaux
bidimensionnels
Un peu de
numpy.array
Création, indexation
IBasique :
>>> t = [10, 12, 15, 7]
>>> len(t)
4
>>> t[2]
15
>>> t[4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> t[-1]
7
IMais aussi :
>>> [0] * 5
[0, 0, 0, 0, 0]
>>> [1 for i in range(3)]
[1, 1, 1]
>>> [i**2 for i in range(3)]
[0, 1, 4]
>>> [2 for _ in range(3)]
[2, 2, 2]
Tableaux/listes en
Python (2/2)
Stéphane Gonnord
Plan
Tableaux et listes en
Python
Tableaux
bidimensionnels
Un peu de
numpy.array
Slicing
Plein de découpages !
IDe base :
>>> t = [3, 5, 12, 15, 42]
>>> t[1: 3]
[5, 12]
IDepuis/jusqu’au bout
>>> t[ : 3]
[3, 5, 12]
>>> t[2: ]
[12, 15, 42]
IAvec un pas (6=1)
>>> t[0: 4: 2]
[3, 12]
>>> t[4: 0: -2]
[42, 12]
Tableaux/listes en
Python (2/2)
Stéphane Gonnord
Plan
Tableaux et listes en
Python
Tableaux
bidimensionnels
Un peu de
numpy.array
Recopie
ISurtout pas :
>>> t = [3, 5, 12, 15]
>>> t1 = t
>>> t1[1] = -1000
>>> t, t1
([3, -1000, 12, 15], [3, -1000, 12, 15])
IMais plutôt :
>>> t2 = t[ : ]
>>> t2[2] = 19
>>> t [1] = 1
>>> t, t1, t2
([3, 1, 12, 15], [3, 1, 12, 15], [3, -1000, 19, 15])
IOu encore :
>>> t3 = [t[i] for i in range(len(t))]
>>> t4 = [ x for x in t]
>>> import copy
>>> t5 = copy.copy(t)
>>> t6 = copy.deepcopy(t)
1 / 16 100%

Tableaux/listes en Python (2/2) - Matrices, et sombres histoires de

La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !