-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethods-SET.py
More file actions
210 lines (140 loc) · 4.01 KB
/
Methods-SET.py
File metadata and controls
210 lines (140 loc) · 4.01 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
########## METHODS SETs ##########
### .add()
# Adds an element to the set
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits)
# {'cherry', 'banana', 'orange', 'apple'}
### .clear()
# Removes all the elements from the set
fruits = {"apple", "banana", "cherry"}
fruits.clear()
print(fruits)
# set() ==> set vide
### .copy()
# Returns a copy of the set
fruits = {"apple", "banana", "cherry"}
x = fruits.copy()
print(x)
# {'banana', 'apple', 'cherry'}
### .difference()
# Returns a set containing the difference between two or more sets
# a.difference(b) Éléments dans a mais pas dans b
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y) # = ce qui est dans x et pas dans y
print(z)
# {'cherry', 'banana'}
a = {1, 2, 3, 4, 5, 6}
b = {3, 4, 5}
print(a.difference(b))
# {1, 2, 6}
### .difference_update()
# Removes the items in this set that are also included in another, specified set
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.difference_update(y) # enleve de x ce qui est dans y
print(x)
# {'banana', 'cherry'}
### .discard()
# Remove the specified item
# Si x n’est pas dans le set, ne fait rien et ne lève pas d’erreur CONTRAIREMENT à remove()
fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
print(fruits)
# {'apple', 'cherry'}
fruits.discard("caca") # n'existe pas mais pas d'erreur
print(fruits)
# {'apple', 'cherry'}
### .intersection()
# Returns a set, that is the intersection of two other sets
a = {1, 2, 3, 4, 5, 6}
b = {2, 6}
print(a.intersection(b))
# {2, 6}
### .intersection_update()
# Removes the items in this set that are not present in other, specified set(s)
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.intersection_update(y) # laisse dans x que ce qui est dans y
print(x)
# {'apple'}
### .isdisjoint()
# Returns whether two sets have a intersection or not
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "facebook"}
z = x.isdisjoint(y)
print(z)
# True
x = {"apple", "banana", "facebook"}
y = {"google", "microsoft", "facebook"}
z = x.isdisjoint(y)
print(z)
# False
### .issubset()
# Returns whether another set contains this set or not
x = {"a", "b", "c"}
y = {"f", "e", "d", "c", "b", "a"}
z = x.issubset(y)
print(z)
# True
x = {"z", "t", "y"}
y = {"f", "e", "d", "c", "b", "a"}
z = x.issubset(y)
print(z)
# False
### .issuperset()
# Returns whether this set contains another set or not
x = {"f", "e", "d", "c", "b", "a"}
y = {"a", "b", "c"}
z = x.issuperset(y)
print(z)
# True
x = {"f", "e", "d", "c", "b", "a"}
y = {"z", "t", "y"}
z = x.issuperset(y)
print(z)
# False
### .pop()
# Removes an element from the set
fruits = {"apple", "banana", "cherry"}
fruits.pop()
print(fruits)
# {'cherry', 'apple'}
### .remove()
# Removes the specified element
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits)
# {'cherry', 'apple'}
fruits.remove("caca") # n'existe pas ==> KeyError
print(fruits)
# KeyError: 'caca'
### symmetric_difference()
# returns a set that contains all items from both set but not the items that are present in both sets
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.symmetric_difference(y)
print(z)
# {'cherry', 'google', 'microsoft', 'banana'}
### symmetric_difference_update()
# Remove the items that are present in both sets, AND insert the items that is not present in both sets
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.symmetric_difference_update(y)
print(x)
{'banana', 'google', 'microsoft', 'cherry'}
### union()
# Return a set containing the union of sets
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.union(y)
print(z)
# {'apple', 'microsoft', 'banana', 'google', 'cherry'}
### update()
# Rajoute dans x ce qui est dans y et pas dans x
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.update(y)
print(x)
# {'google', 'apple', 'microsoft', 'cherry', 'banana'}