集合及深浅copy 02
集合
求交集
#求交集 set1 = {1,2,3,4,5} set2 = {3,4,5,6,7} print(set1 & set2) print(set2.intersection(set1))
求并集
#求并集 set1 = {1,2,3,4,5} set2 = {3,4,5,6,7} print(set1 | set2) print(set2.union(set1))
反交集
#求反交集 set1 = {1,2,3,4,5} set2 = {3,4,5,6,7} print(set1 ^ set2) print(set2.symmetric_difference(set1))
差集
#差集 set1 = {1,2,3,4,5} set2 = {3,4,5,6,7} print(set1 - set2) print(set1.difference(set2))
子集与超集
set1 = {1,2,3} set2 = {1,2,3,4,5,6,7} print(set1 < set2) print(set2.issubset(set1))
#超集 set1 = {1,2,3} set2 = {1,2,3,4,5,6,7} print(set1 < set2) print(set2.issuperset(set1))
去重复
set1 = [1,2,3,4,5,6,76,8,23,1,2,3,4,5] li = set(set1) print(li) set1 = list(li) print(set1)
把集合变为不可变集合
s = frozenset("hksahdasl") print(s,type(s))