集合的方法
1.set.add()
set1 = {'a','b',1,2}
set1.add(3)
print(set1)
2.set.copy()
复制的是值,不是地址
set2= set1.copy()
set2
3.set.clear()
set1.clear()
print(set1)
print(set2)
4.x.difference(y)
集合的差集 ,我们可用来 做 数据的不同数据的 处理
x = {'a','b','c',1,2,3}
y = {'a',1,'d'}
print( x.difference(y) )
5.x.difference_update(y)
求集合的差集,返回的新值 会覆盖 x
print(x.difference_update(y))
print(x)
6.set.discard()
删除 set中的元素,删除的不存在的话 ,不报错
set3 = {'a','b',3,4}
set3.discard('a')
set3.discard('c')
print(set3)
7.set.remove()
删除不存在的会报错
set3.remove(4)
# set3.remove(5)
print(set3)
7.set.remove()
删除不存在的会报错
set3.remove(4)
# set3.remove(5)
print(set3)
8.set.pop()
在运行效果中,pop() 是删除最后一个。可是为什么说是删除随机元素, 因为 set,是无序的 ,你不知道最后一个到底是谁,所以在一定程度上也可以说是删除随机的
set3.pop()
print(set3)
9.x.intersection(y)
返回两个集合的交集
x = {'a','b','c',1,2,3}
y = {1,'a'}
print( x.intersection(y) )
10.x.intersection_update(y)
返回两个集合的交集,并覆盖 x
x = {'a','b','c',1,2,3}
y = {1,'a'}
print(x.intersection_update(y))
print(x)
11.x.isdisjoint(y)
如果 x 与 y 有相同的则返回 False ,没有相同的则返回 True,或者 说成 x y 是否不一样
x = {'a','b','c',1,2,3}
y = {1,'a'}
print( x.isdisjoint(y) )
13.x.issuperset(y)
判断 y 中的所有是否都在 x 中。
x = {'a','b','c',1,2,3}
y = {1,'a'}
print( x.issuperset(y))