-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsets.py
63 lines (35 loc) · 1.26 KB
/
sets.py
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
# lets define some lists and sets
a = [1, 2, 3]
b = [4, 1, 1, 3, 5]
# sets can be defined using set literals
my_set = {1, 2, 3}
# sets can be also defined using set comprehensions
my_set = {x for x in sequence}
# notice that there are only unique items in the sets
sa = set(a)
sa # >>> set([1, 2, 3])
sb = set(b)
sb # >>> set([1, 3, 4, 5])
# check if value exists in set
2 in sb
# is every element in the "sa" in "sb"
sa.issubset(sb) # >>> False
# is every element in "sb" in the "sa"
sa.issuperset(sb) # >>> False
# return True if there are no common elements in "sa" and "sb"
sa.isdisjoint(sb) # >>> False
# intersection, return a new set with elements common to the set and all others
sa & sb # >>> set([1, 3])
# union, return a new set with elements from the set and all others
sa | sb # >>> set([1, 2, 3, 4, 5])
# symmetric_differencem, return a new set with elements in either the set or other but not both
sa ^ sb # >>> set([2, 4, 5])
# difference, return a new set with elements in the set that are not in the others
sa - sb # >>> set([2])
# add and remove items from set
my_set.add(2)
my_set.remove(3)
# clean the whole set
my_set.clear()
# frozent set is like set, but once you initialize it you cant change it (immutable)
my_frozen_set = frozenset(my_set)