set1 = {1, 'alex', 'barry', 3, 5}
print(set1)
increase:
set1 = { 'Taibaijinxing', 'crystal goddess', 'Alex', 'Wu'}
add
set1.add('ss')
print (set1) # { 'Wu', 'Taibaijinxing', 'Alex', 'crystal goddess', 'ss'}
update # down with increase
set1.update('yuanhao123')
print (set1) # { 'alex', 'a', 'crystal goddess',' 3 ',' o ',' n ',' Taibaijinxing ',' Wu ',' h ',' 2 ',' 1 ',' y ',' u '}
delete
remove deleted in accordance with the elements
set1.remove('alex')
print (set1) # { 'Taibaijinxing', 'crystal goddess', 'Wu'}
pop delete random
set1.pop()
print (set1) # {first 'Alex', 'crystal goddess', 'Wu'} {Second 'Taibaijinxing', 'Alex', 'Wu'}
The value in disguise
set1.remove ( 'Taibaijinxing')
set1.add ( 'god')
print (set1) # { 'alex', 'crystal goddess', 'Wu', 'god'}
Intersection
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 & set2) #{4, 5}
Union
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set2 | set1) {1, 2, 3, 4, 5, 6, 7, 8}
Difference set
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 - set2) #{1, 2, 3}
print(set2 - set1) #{8, 6, 7}
Anti intersection
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 ^ set2) #{1, 2, 3, 6, 7, 8}
Subset and superset
set1 = {1,2,3}
set2 = {1,2,3,4,5,6}
print (set1.issubset (set2)) #True # two identical, are described set1 set2 is a subset of
# Two identical print (set1 <set2), are described set1 set2 is a subset of
# Two identical print (set2> set1), are described set1 set2 is a superset
Interview questions
l1 = [1, 2, 2, 1, 3, 5, 6, 8, 8, 9]
l2 = list(set(l1))
print(l2) #[1, 2, 3, 5, 6, 8, 9]