-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathType-DICTIONARY.py
More file actions
193 lines (146 loc) · 4.92 KB
/
Type-DICTIONARY.py
File metadata and controls
193 lines (146 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
########## DICTIONARY ##########
### Dictionaries are unordered, changeable (mutable), and indexed collections of data.
# In Python 3.6x dictionaries have become ordered by default.
## A dictionary is a set of key-value pairs
# 1- each key must be unique - it's not possible to have more than one key of the same value
# 2- a key may be data of any type: it may be a number (integer or float), or even a string
# 3- a dictionary is not a list - a list contains a set of numbered values, while a dictionary holds pairs of values
# 4- the len() function works for dictionaries, too - it returns the numbers of key-value pairs in the dictionary
# 5- a dictionary is a one-way tool - if you have an English-French dictionary, you can look for French equivalents of English terms
# but not vice versa
# 6- fully mutable meaning you can modify delete and add values
### create dictionaries
dict = {"cat" : "chat", "dog" : "chien", "horse" : "cheval"}
phoneNumbers = {'boss' : 5551234567, 'Suzy' : 22657854310}
emptyDictionary = {}
print(dict)
# {'dog': 'chien', 'horse': 'cheval', 'cat': 'chat'}
print(phoneNumbers)
# {'Suzy': 5557654321, 'boss': 5551234567}
print(emptyDictionary)
# {}
### Penser à définir les variables au préalable avant de mettre dans la liste sinon NameError
# les keys doivent etre des strings ou numbers par exemple; si variable, il faut la définir avant
myDictionary = {
key1 : value1,
key2 : value2,
key3 : value3,
}
# NameError: name 'key1' is not defined
value1 = 1
value2 = 2
value3 = 3
myDictionary = {
"key1" : value1,
"key2" : value2,
"key3" : value3,
}
print(myDictionary)
# {'key1': 1, 'key2': 2, 'key3': 3}
### une paire key/value peut etre comparée à une liste
colors = {
"white" : (255, 255, 255),
"grey" : (128, 128, 128),
"red" : (255, 0, 0),
"green" : (0, 128, 0)
}
for col, rgb in colors.items():
print(col, ":", rgb)
# white : (255, 255, 255)
# grey : (128, 128, 128)
# red : (255, 0, 0)
# green : (0, 128, 0)
### retrouver des items : mentionner la clé
dict = {"cat" : "chat", "dog" : "chien", "horse" : "cheval"}
phoneNumbers = {'boss' : 5551234567, 'Suzy' : 22657854310}
emptyDictionary = {}
print(dict['cat'])
# chat
print(phoneNumbers['Suzy'])
# 22657854310
# PAS d'indexation !
phoneNumbers = {'boss' : 5551234567, 'Suzy' : 22657854310}
print(phoneNumbers[1])
# KeyError
# PAS PLUS D'1 KEY
data = {'a': 1, 'b': 2, 'c': 3}
print(data['a', 'b']) # les key a et b
# KeyError: ('a', 'b')
### for + dictionnaire
dict = {"cat" : "chat", "dog" : "chien", "horse" : "cheval"}
words = ['cat', 'lion', 'horse']
for word in words:
if word in dict:
print(word, "->", dict[word])
else:
print(word, "is not in dictionary")
# cat -> chat
# lion is not in dictionary
# horse -> cheval
### modifier une value
dict = {"cat" : "chat", "dog" : "chien", "horse" : "cheval"}
dict['cat'] = 'minou'
print(dict)
# {'cat': 'minou', 'dog': 'chien', 'horse': 'cheval'}
### ajouter un item
dict = {"cat" : "chat", "dog" : "chien", "horse" : "cheval"}
dict['swan'] = 'cygne'
print(dict)
# {'cat': 'chat', 'dog': 'chien', 'horse': 'cheval', 'swan': 'cygne'} # ajoute à la fin
### delete une key
dict = {"cat" : "chat", "dog" : "chien", "horse" : "cheval"}
del dict['dog']
print(dict)
# {'cat': 'chat', 'horse': 'cheval'}
## removes the dictionary
dict = {"cat" : "chat", "dog" : "chien", "horse" : "cheval"}
del dict
print(dict)
# NameError
## 1 = 1.0 =! "1"
data = {}
data[1] = 1
data['1'] = 2
data[1.0] = 4 # ici data[1] = data[1.0] donc 1 est écrasé par 4
print(data)
# {1: 4, '1': 2}
res = 0
for d in data:
res += data[d]
print(res)
# 6
### copy between dictionaries
# SHALLOW copy
import copy
a_dict = {
'first name': 'James',
'last name': 'Bond',
'movies': ['Goldfinger (1964)', 'You Only Live Twice']
}
b_dict = (a_dict)
print('Memory chunks:', id(a_dict), id(b_dict))
# Memory chunks: 2452251144960 2452251144960
print('Same memory chunk?', a_dict is b_dict)
# Same memory chunk? True
a_dict['movies'].append('Diamonds Are Forever (1971)')
print('a_dict movies:', a_dict['movies'])
# a_dict movies: ['Goldfinger (1964)', 'You Only Live Twice', 'Diamonds Are Forever (1971)']
print('b_dict movies:', b_dict['movies'])
# b_dict movies: ['Goldfinger (1964)', 'You Only Live Twice', 'Diamonds Are Forever (1971)']
# DEEP copy
import copy
a_dict = {
'first name': 'James',
'last name': 'Bond',
'movies': ['Goldfinger (1964)', 'You Only Live Twice']
}
b_dict = copy.deepcopy(a_dict)
print('Memory chunks:', id(a_dict), id(b_dict))
# Memory chunks: 2349147026176 2349144587520
print('Same memory chunk?', a_dict is b_dict)
# Same memory chunk? False
a_dict['movies'].append('Diamonds Are Forever (1971)')
print('a_dict movies:', a_dict['movies'])
# a_dict movies: ['Goldfinger (1964)', 'You Only Live Twice', 'Diamonds Are Forever (1971)']
print('b_dict movies:', b_dict['movies'])
# b_dict movies: ['Goldfinger (1964)', 'You Only Live Twice']