Python集合常用操作
-
- 增加和修改操作
- 删除操作
- 运算操作(集合的交、并、差等操作)
集合是一个无序的,不重复的数据类型
增加和修改操作
my_set = {1,2,3,5,6,7,8}
my_set.add(9)
print(my_set)
my_set.update({666,999})
print(my_set)
删除操作
my_set = {1,2,3,5,6,7,8}
my_set.remove(3)
print(my_set)
my_set.pop()
print(my_set)
my_set.clear()
print(my_set)
del my_set
my_set.discard(2)
print(my_set)
运算操作(集合的交、并、差等操作)
my_set1 = {1,2,3,4,5,6,7,8}
my_set2 = {5,6,7,8,9,10}
my_set3 = {1,2,3,4}
my_set4 = {666,777}
print(my_set1.intersection(my_set2))
print(my_set1 & my_set2)
print(my_set1.union(my_set2))
print(my_set1 | my_set2)
print(my_set1.difference(my_set2))
print(my_set1 - my_set2)
print(my_set2.difference(my_set1))
print(my_set2 - my_set1)
print(my_set1.issubset(my_set2))
print(my_set3.issubset(my_set1))
print(my_set1.issuperset(my_set2))
print(my_set1.issuperset(my_set3))
print(my_set1.symmetric_difference(my_set2))
print(my_set1 ^ my_set2)
my_set1.isdisjoint(my_set2)
my_set1.isdisjoint(my_set4)