# 集合会自己去重 set1 = set([1, 2, 3, 4, 5, 1, 2]) set2 = set([4, 5, 6, 7, 8]) print(set1) # 查询 # 查询具体值只能通过for循环去遍历 print(1 in set1) # 判断是否在集合中 print(1 not in set1) # 添加 # set1.add("123") # 添加单个数据 # # print(set1) # # set1.update("qwer") # 添加一个序列数据 # print(set1) # 删除 # set1.remove("r") # 删除一个数据 # print(set1) # set1.pop() # 随机删除一个数据 # print(set1) # set1.clear() # 清空集合 # print(set1) # del set1 # 删除集合 # 交集 print(set1.intersection(set2)) print(set1 & set2) # 并集 print(set1.union(set2)) print(set1 | set2) # 差集 print(set1.difference(set2)) print(set1 - set2) print(set2.difference(set1)) print(set2 - set1) # 对称差集 print(set1.symmetric_difference(set2)) print(set1 ^ set2) # 判断后一个集合是否包含前一个集合 print(set([1, 2]) < set1) print(set([1, 2]).issubset(set1)) # 判断前一个集合是否包含后一个集合 print(set1.issuperset(set([1, 2]))) print(set1 > (set([1, 2])))
标签:set,print,set1,set2,集合,基本操作,difference From: https://www.cnblogs.com/GDquicksand/p/18374224