-
Notifications
You must be signed in to change notification settings - Fork 82
/
sets.py
59 lines (49 loc) · 2.04 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
#Using python to manipulate sets
'''
Python knows a number of compound data types,
used to group together other values.
They are:
tuple
dictionary
set
list
A Set is an unordered collection with no duplicate elements. Like a dictionary,
a set is defined by a curly bracket.
Sets are mutable - this means that items can be changed.
Set has a whole bunch of methods available.
add() Adds an element to the set
clear() Removes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set containing the difference between two or more sets
difference_update() Removes the items in this set that are also included in another, specified set
discard() Remove the specified item
intersection() Returns a set, that is the intersection of two or more sets
intersection_update() Removes the items in this set that are not present in other, specified set(s)
isdisjoint() Returns whether two sets have a intersection or not
issubset() Returns whether another set contains this set or not
issuperset() Returns whether this set contains another set or not
pop() Removes an element from the set
remove() Removes the specified element
symmetric_difference() Returns a set with the symmetric differences of two sets
symmetric_difference_update() Inserts the symmetric differences from this set and another
union() Return a set containing the union of sets
update() Update the set with another set, or any other iterable
'''
#The basics
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)# show that duplicates have been removed
'orange' in basket # fast membership testing
'crabgrass' in basket
# Demonstrate set operations on unique letters from two words
a = set('abracadabra')
b = set('alacazam')
a # unique letters in a
a - b # letters in a but not in b
a | b # letters in a or b or both
a & b # letters in both a and b
a ^ b # letters in a or b but not both
#Set comprehension
a = {x for x in 'abracadabra' if x not in 'abc'}
#built-in function set()
x = set(('bobby','bobby', 'at', 'didcoding','dot', 'com')) # creates a set object
x