set1 = {"周星驰","周杰伦","周润发","王文"} set2 = {"王健林","王思聪","王宝强","王文"} # intersection() 交集 res = set1.intersection(set2) print(res) res = set1 & set2 print(res) # difference() 差集 res = set1.difference(set2) print(res) res = set2.difference(set1) print(res) res = set1 - set2 print(res) # union() 并集 res = set1.union(set2) print(res) res = set1 | set2 print(res) # symetic_difference() 对称差集 res = set1.symmetric_difference(set2) print(res) res = set1 ^ set2 print(res) # issubset() 是否是子集 ,完全包含其中的情况 set_father = {"周星驰","周杰伦","周润发","王文"} set_son = {"周杰伦","周润发"} res = set_son.issubset(set_father) print(res) res = set_son < set_father print(res) # issuperset() 判断是父集 set_father = {"周星驰","周杰伦","周润发","王文"} set_son = {"周杰伦","周润发"} res = set_father.issuperset(set_son) print(res) res = set_father > set_son print(res) set_father = {"周星驰","周杰伦","周润发","王文"} set_son = {"周星驰","周杰伦","周润发","王文"} res = set_father >= set_son print(res) res = set_father <= set_son print(res) # isdisjoint() 是否不相交 不相交 True 相交False set_father = {"周星驰","周杰伦","周润发","王文"} set_son = {"周星驰","周杰伦"} res = set_father.isdisjoint(set_son) print(res) # add() 增 setvar = {"刘浩杰","徐信信","何伟福","林志远"} setvar.add("湖滨") print(setvar) # update() 迭代着追加 setvar = {"刘浩杰","徐信信","何伟福","林志远"} lst = ["胡家豪","胡启超"] setvar.update(lst) print(setvar) setvar = {"刘浩杰","徐信信","何伟福","林志远"} strvar = "abcd" setvar.update(strvar) print(setvar) # clear() 清空 setvar = {"刘浩杰","徐信信","何伟福","林志远"} setvar.clear() print(setvar) # pop() 随机删除集合一个数据 setvar = {"刘浩杰","徐信信","何伟福","林志远"} res = setvar.pop() print(res) print(setvar) # remove() 删除集合中指定值 ,不存在报错 setvar = {"刘浩杰","徐信信","何伟福","林志远"} setvar.remove("徐信信") print(setvar) # discard() 如果值不存在不删除,也不报错 setvar = {"刘浩杰","徐信信","何伟福","林志远"} setvar.discard("何伟福123123334") print(setvar) # ###冰冻集合 fz = frozenset() print(fz,type(fz)) fz1 = frozenset([1,"2",3,4]) fz2 = frozenset("7892") print(fz1,fz2) # 冰冻集合只能做交叉并补 res = fz1 & fz2 print(res) # fz1.add(4) 报错 无增加删除操作