-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsets.py
More file actions
495 lines (408 loc) · 12.3 KB
/
Copy pathsets.py
File metadata and controls
495 lines (408 loc) · 12.3 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# Create a set:
set = {"apple", "banana", "cherry"}
print(set)
# Output: {'apple', 'banana', 'cherry'}
# Duplicate values will be ignored:
set = {"apple", "banana", "cherry", "apple"}
print(set)
# Output: {'apple', 'banana', 'cherry'}
# True and 1 are considered the same:
set = {"apple", "banana", "cherry", True, 1, 2}
print(set)
# False and 0 are considered the same:
set = {"apple", "banana", "cherry", False, 0, 2}
print(set)
# Output: {'apple', 'banana', 'cherry', 2, False}
# Get the length of a set:
set = {"apple", "banana", "cherry"}
print(len(set))
# Output: 3
# Set items can be of any data type:
set = {"apple", "banana", "cherry", 1, 2, 3}
print(set)
# Output: {'apple', 'banana', 'cherry', 1, 2, 3}
# type() can be used to check the type of a set:
set = {"apple", "banana", "cherry"}
print(type(set))
# Output: <class 'set'>
# Access set items
# Loop through the set, and print the values:
set={"apple", "banana", "cherry"}
for x in set:
print(x)
# Check if "banana" is present in the set:
set={"apple", "banana", "cherry"}
print("banana" in set)
# Check if "banana" is NOT present in the set
set={"apple","banana", "cherry"}
print("banana" not in set)
# Add set items
# Add an item to a set, using the add() method.
set={'apple','banana','cherry'}
set.add('orange')
print(set)
# update() method:
# Add elements from tropical into this_set :
this_set={'apple', 'banana', 'cherry'}
tropical={'pineapple', 'mango', 'papaya'}
this_set.update(tropical)
print(this_set)
# Remove set items
this_set={'apple', 'banana', 'cherry'}
this_set.remove('banana')
print(this_set)
# Add any element of a list to at set:
this_set={'apple', 'banana', 'cherry'}
my_list=['kiwi', 'orange']
this_set.update(my_list)
print(this_set)
# Remove set items
# Remove 'banana' by using the remove() method.
this_set={'apple','banana','cherry'}
this_set.remove('banana')
print(this_set)
# Remove 'banana' by using the discard() method.
this_set={'apple', 'banana', 'cherry'}
this_set.discard('banana')
print(this_set)
# Remove random item by using the pop() method:
this_set={'apple', 'banana', 'cherry'}
x=this_set.pop()
print(x)
# The clear() method empties the set:
this_set={'apple','banana','cherry'}
this_set.clear()
print(this_set)
# Loop items
this_set={'apple', 'banana','cherry'}
for x in this_set:
print(x)
# Join sets:
# union() methods:
# Join set1 and set2 into a new set:
set1={'a', 'b', 'c'}
set2={1,2,3}
set3=set1.union(set2)
print(set3)
# You can use the | operator instead of the union() method, and you will get the same result.
# Use | to join two sets.
set1={'a', 'b', 'c'}
set2={1, 2, 3}
set3= set1 | set2
print(set3)
# Join multiple sets
# Join multiple set with the union() method:
set1={'a', 'b', 'c'}
set2={1, 2, 3}
set3={'John', 'Elena'}
set4={'apple', 'banana', 'cherry'}
my_set=set1.union(set2, set3, set4)
print(my_set)
# Use | to join two operators:
set1={'a','b','c'}
set2={1,2,3}
set3={'john','Elena'}
set4={'apple','banana','cherry'}
my_set=set1|set2|set3|set4
print(my_set)
# join a set with tuple
x={'a','b','c'}
y={1,2,3}
z=x.union(y)
print(z)
# update() method
# The update() method inserts the items in set2 into set1
set1={'a','b','c'}
set2={1,2,3}
set1.update(set2)
print(set1)
# Intersection
# Keep only the duplicates
# Join set1 and set2, but keep only the duplicates.
set1={'apple','banana','cherry'}
set2={1,2,'apple'}
set3=set1.intersection(set2)
print(set3)
# You can use the & operator instead of the intersection() method, and you will get the same result:
# Use & to join two sets.
set1={'apple','banana','cherry'}
set2={'google','microsoft','apple'}
set3=set1&set2
print(set3)
# The intersection_update method will also keep ONLY the duplicates, but it will change the original set instead of returning a new set.
set1={'apple','banana','cherry'}
set2={'google','microsoft','apple'}
set1.intersection_update(set2)
print(set1)
# Join sets that contains the value True,False,1 and 0, and see what is considered as duplicates.
set1={'apple',True,'banana',0,'cherry'}
set2={False,'google','apple',2,True}
set3=set1.intersection(set2)
print(set3)
# Difference
# Keep all items from set1 that are not in set2:
set1={'apple','banana','cherry'}
set2={'google','microsoft','apple'}
set3=set1.difference(set2)
print(set3)
# You can use the - operator instead of difference() method, and you will get the same result.
set1={'apple','orange','banana'}
set2={'apple',1,2}
set3=set1-set2
print(set3)
# Use the difference_update() to keep the items that are not present in both sets.
set1={'apple','banana','cherry'}
set2={'google','microsoft','apple'}
set1.difference_update(set2)
print(set1)
# Symmetric Differences:
# The symmetric_difference() method will keep only the elements that are NOT present in both sets.
# Keep the items that are not present in both sets:
set1={'apple','banana','cherry'}
set2={'google','microsoft','apple'}
set3=set1.symmetric_difference(set2)
print(set3)
# you can use the ^ operator instead of the symmetric_difference() method, and you will get the same result:
# Use ^ to join two sets:
set1={'apple','banana','cherry'}
set2={'google','microsoft','apple'}
set3=set1^set2
print(set3)
# The symmetric_difference_update() method will also keep all but the duplicates, but it wil change the original set instead of returning a new set.
# Use the symmetric_difference_update() method to keep the items that are not present in both sets:
set1={'apple','google','cherry'}
set2={'banana', 'microsoft','apple'}
set1.symmetric_difference_update(set2)
print(set1)
# Set Methods
# Python has a set of built-in methods that you can use on sets.
# Add an element to the fruits set:
# add()
fruits={'apple', 'banana', 'cherry'}
fruits.add('orange')
print(fruits)
# Try to add an element that already exists.
fruits={'apple', 'banana', 'cherry'}
fruits.add('apple')
print(fruits)
# clear() method:
fruits={'apple', 'banana', 'cherry'}
fruits.clear()
print(fruits)
# copy() method.
fruits={'apple','banana', 'cherry'}
x=fruits.copy()
print(x)
# difference() method:
# Return a set that contains the items that only exist in set x, and not in set y:
x={'apple','banana','cherry'}
y={'google','microsoft','apple'}
z=x.difference(y)
print(z)
# Use - as a shortcut instead of difference():
a={'apple', 'banana', 'cherry'}
b={'google','microsoft','apple'}
my_set=a-b
print(my_set)
# Join more than two sets:
a={'apple','banana','cherry'}
b={'google','microsoft','apple'}
c={'cherry','microsoft','bluebird'}
my_set=a.difference(b,c)
print(my_set)
# Join more than two sets with the - operator:
a={'apple','banana','cherry'}
b={'google','microsoft','apple'}
c={'cherry','microsoft','bluebird'}
my_set=a-b-c
print(my_set)
# Reverse the example on the top of this page. Return a set that contains the items that only exist in set y,and not in set x:
x={'apple', 'banana', 'cherry'}
y={'google', 'microsoft', 'apple'}
z=y.difference(x)
print(z)
# difference_update()
x={'apple','banana','cherry'}
y={'google','microsoft','apple'}
x.difference(y)
print(x)
# join more than two sets:
a={'apple','banana','cherry'}
b={'google','microsoft','apple'}
c={'cherry','micro','bluebird'}
a.difference_update(b,c)
print(a)
# Join more than two sets with the -= operator.
a={'apple','banana','cherry'}
b={'google','microsoft','apple'}
c={'cherry','micro','bluebird'}
a-=b|c
print(a)
# discard() Method:
fruits={'apple', 'banana', 'cherry'}
fruits.discard('banana')
print(fruits)
# intersection() method:
# Return a set that contains the items that exist in both set x, and set y:
x={'apple','banana','cherry'}
y={'google','microsoft','apple'}
z=x.intersection(y)
print(z)
# Use & as a shortcut instead of intersection():
x={'apple','banana','cherry'}
y={'google','microsoft','apple'}
z=x & y
print(z)
# Join 3 sets, and a return a set with items that is present in all 3 sets:
x={'a', 'b', 'c'}
y={'c', 'd', 'e'}
z={'f','g','c'}
result=x.intersection(y,z)
print(result)
# intersection_update() [&=]
# Remove the items that is not present in both x and y:
x={'google','banana','cherry'}
y={'google','microsoft','apple'}
x.intersection_update(y)
print(x)
# Use &= as a shortcut instead of intersection_update()
x={'apple','banana','cherry'}
y={'google','microsoft','apple'}
x&=y
print(x)
# Compare 3 sets, and return a set with items that is present in all 3 sets:
x = {"a", "b", "c"}
y = {"c", "d", "e"}
z = {"f", "g", "c"}
x &= y & z
print(x)
# isdisjoint() Method - Returns whether two sets have a intersection or not.
# Return True if no items in set x is present in set y:
x={'apple', 'banana', 'cherry'}
y={'google','microsoft','facebook'}
z=x.isdisjoint(y)
print(z)
# What if one or more items are present in both sets?
# In this example, 'apple' is present in both sets:
x={'apple','banana','cherry'}
y={'google','microsoft','apple'}
z=x.isdisjoint(y)
print(z)
# issubset() MEthod - Returns True if all items of this set is present in another set.
# Return True if all items in set x are present in set y:
x={'a','b','c'}
y={'f','e','d','c','b','a'}
z=x.issubset(y)
print(z)
# Use <= a shortcut instead of issubset():
x={'a','b','c'}
y={'f','e','d','c','b','a'}
z=x<=y
print(z)
# What if not all items are present in the specified set?
# Returns false if not all items in set x are present in set y:
x={'a','b','c'}
y={'f','e','d','c','b'}
z=x.issubset(y)
print(z)
# < - Returns True is all items of this set is present in another, larger set.
# Returns False even if all items in set x are present in set y:
x={'a','b','c'}
y={'c','b','a'}
z= x < y
print(z)
# The < returns True if all items in the set exists in s specified larger set, otherwise it returns false.
# set1 < set2
# Returns True because all items of set x is present in the larger set y:
x={'a','b','c'}
y={'f','e','d','c','b','a'}
z=x<y
print(z)
# issuperset() Method [>=] - Returns True if all items of another set is present in this set.
# Returns True if all items in set x is present in set y:
x={'f','e','d','c','b','a'}
y={'a','b','c'}
z=x.issuperset(y)
print(z)
# Use >= as a shortcut instead of issuperset():
x={'f','e','d','c','b','a'}
y={'a','b','c'}
z=x>=y
print(z)
# What if not all items are present in the specified set?
# Returns false if not all items in set x are present in set y:
x={'f','e','d','c','b'}
y={'a','b','c'}
z=x.issuperset(y)
print(z)
# Returns True if all items in set x are present in set y:
x={'a','b','c'}
y={'b','a'}
z=x > y
print(z)
# The > returns True if all items in the set exists in a specified larger set, otherwise it returns false.
# set1 > set2
x={'c','b','a'}
y={'b','a','c','d','e','f'}
z=x>y
print(z)
# pop() Method - Removes a random item from the set.
# Remove a random item from the set:
x={'apple', 'banana', 'cherry'}
x.pop()
print(x)
# remove() Method - Removes the specified item from the set.
x={'apple', 'banana', 'cherry'}
x.remove('banana')
print(x)
# symmetric_difference() Method - [^] Returns a set with the symmetric differences of two sets.
# Return a set that contains all items from both sets, except items that are present in both sets.
x={'apple', 'banana', 'cherry'}
y={'google', 'microsoft', 'apple'}
z=x.symmetric_difference(y)
print(z)
# symmetric_difference_update() Method - [^=] Returns a set with the symmetric differences of two sets, and updates the original set.
x={'apple', 'banana', 'cherry'}
y={'google', 'microsoft', 'apple'}
x.symmetric_difference_update(y)
print(x)
# Use ^= as a shortcut instead of symmetric_difference_update():
x={'google', 'banana', 'cherry'}
y={'google', 'microsoft', 'apple'}
x ^= y
print(x)
# union() Method - Returns a set that contains all items from both sets, duplicates are excluded.
# Return a set that contains all items from both sets:
x={'apple', 'banana', 'cherry'}
y={'google', 'microsoft', 'apple'}
z=x.union(y)
print(z)
# Unify more than two sets:
x={'a', 'b', 'c'}
y={'f','d','a'}
z={'c', 'd', 'e'}
result=x.union(y, z)
print(result)
# update() Method - Updates the set with the union of this set and another set.
# Update the set with the union of this set and another set:
x={'apple', 'banana', 'cherry'}
y={'google', 'microsoft', 'apple'}
x.update(y)
print(x)
# Use |= as a shortcut instead of update():
x={'apple', 'banana', 'cherry'}
y={'google', 'microsoft', 'apple'}
x |= y
print(x)
# Insert more than one set
x={'apple','banana','cherry'}
y={'google','microsoft','apple'}
z={'cherry', 'microsoft', 'bluebird'}
x.update(y, z)
print(x)
# Join more than two sets with the |= operator:
x={'apple','banana','cherry'}
y={'google','microsoft','apple'}
z={'cherry', 'microsoft', 'bluebird'}
x |= y | z
print(x)