Python-数据类型-set

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)