Dictionnaires!
Un dictionnaire est un ensemble de paires key:value. Toutes les clés doivent être uniques.
creation: emptyDict = {}
thisdict = {'a':5, 'b':23, 'c':'eggs'}
accessing: thisdict[‘a’] returns 5
deleting: del thisdict[‘b’]
finding: thisdict.has_key(‘e’) returns True
'e' in thisdict returns True
thisdict.keys() returns [‘a’, ‘c’]
thisdict.items() returns [(‘a’, 1), (‘c’, ‘eggs’)]
Manipulation!des!listes!
Un des types de données les plus importants : très flexible et ayant beaucoup de fonction prédéfinies.
creation: thelist = [5,3,‘p’,9,‘e’] [5,3,’p’,9,’e’]
accessing: thelist[0] returns 5 [5,3,’p’,9,’e’]
slicing: thelist[1:3] returns [3, ‘p’] [5,3,’p’,9,’e’]
thelist[2:] returns [‘p’, 9, ‘e’]
thelist[:2] returns [5, 3]
thelist[2:-1] returns [‘p’, 9]
length: len(thelist) returns 5
sort: thelist.sort() no return value [3,5,9,’e’,’p’]
add: thelist.append(37) [3,5,9,’e’,’p’,37]
return &
remove: thelist.pop() returns 37 [3,5,9,’e’,’p’]
thelist.pop(1) returns 5 [3,9,’e’,’p’]
insert: thelist.insert(2, ‘z’) [3,’z’,9,’e’,’p’]
remove: thelist.remove(‘e’) [3,’z’,9,’p’]
deletion thelist[0] [‘z’,9,’p’]
concatenation: thelist + [0] returns [‘z’,9,’p’,0] [‘z’,9,’p’]
finding: 9 in thelist r returns True [‘z’,9,’p’]
Listes de listes (tableaux à 2 ou plus dimensions)
creation: l2 = [[1,2], [‘a’,’b’],‘p’]
accessing: l2[1] returns [‘a’,’b’]
l2[1][0] returns ‘a’
Creation d’un tableau de taille 3x2
m=[0]*3
for i in xrange(2):
m[i]=[0]*3
print m affiche [[0, 0], [0, 0], [0, 0]]