x = set(["a","b","c","d","e","f","1"])
y = set(["c","a","1","h"])
'''
集合不能通过索引
集合没有访问单个元素的方法
集合不是线性结构, 集合元素没有顺序
'''
print("交集:", x & y )
print("intersection:",x.intersection(y))
print("並集:", x | y )
print("union:",x.union(y))
print("差集:", x - y )
print("difference:",x.difference(y))
# 添加一项
x.add('x')
print("add增加:",x)
# 在s中添加多项
x.update([10, 37, 42])
print("update增加多項;",x)
#remove刪除
x.remove("f")
print("remove刪除:",x)
#如果在 set “s”中存在元素 x, 则删除,不存在則返回原集合
x.discard("p")
print("discard刪除:",x)
#删除并且返回 set x中的一个不确定的元素, 如果为空则引发 KeyError
x.pop()
print("pop刪除",x)
Python-数据类型-set
最新推荐文章于 2024-08-10 23:14:03 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)