# 集合作用:去重,关系测试,不能直接取值
list = {1, 2, 3, 4, 5, 6}
print(list, type(list))
data = [1, 2, 3]
list2 = set(data)
print(list2, type(list2))
print("交集:", list.intersection(list2), list & list2)
print("差集:", list.difference(list2), list - list2)
print("并集:", list.union(list2), list | list2)
print("子集:", list.issubset(list2))
print("父集:", list.issuperset(list2))
print("对称差集:", list.symmetric_difference(list2), list ^ list2) # 在a或b中,但是不会同时出现在两个里面
print("是否没有交集", list.isdisjoint(list2))
list.add("haha") # 增加
print(list)
list.update({"hehe", "hah"}) # 增加多项
print(list)
len(list)
print(1 in list)
print(1 not in list)
list.pop() # 随机删除
list.remove("hehe") # 标准删除
list.discard("hah") # 标准删除
print(list)
# 创建一个空的集合
list3 = set()
print(list3)
list3.add("哈哈")
print(list3)
网友评论