# 1、set集合
# 集合是一个无序不重复元素的序列。
# 基本功能是进行成员关系测试和删除重复元素
# 回忆:
# li = [] 创建一个列表,这种方法在python内部实际是会调用list()这种方法。
# list()创建列表的另外一种方法
# 注:类后面加一个括号(),就是调用了类的_init_方法,执行这个方法的时候,需要接收一个参数。
# 比如在list((11,22,33,44))里面就传了一个元组进来。
# 在_init_内部实际是执行了一个for循环,这个for循环就去循环元组里面的所有元素,然后挨个去创建。
# dic = {"k1":v123} 创建一个字典,key:value是一个键值对,一个键值对代表的是字典的一个元素
# se = {"123","456"} 创建一个集合,和创建一个字典很像都是用大括号{}创建,但是有不一样的地方,不再以键值对作为一个元素,而是写一个什么东西它就是一个元素。
# 1.1、创建集合
# 使用大括号{}或者set()函数创建集合
# s1 = {11,22}
# s2 = set()
# s3 = set([11,22,33,44])
# 注意:创建一个空集合必须用set()而不是{},因为{}是用来创建一个空字典
student = {'zhangsan','lisi','wangwu','zhaosi','zhangsan','wangwu','wangmazi'}
# print(student)
# 输出:
# {'lisi', 'wangwu', 'zhangsan', 'zhaosi', 'wangmazi'}
# 输出集合,重复的元素被自动去掉
# 1.2、操作集合
s1 = set() #创建一个空集合
# li = {} 创建一个空字典
# print(li)
# print(s)
# print(type(s))
# print(type(li))
# 添加元素
# def add(self, *args, **kwargs): # real signature unknown
# """
# Add an element to a set,添加元素
#
# This has no effect if the element is already present.
# """
# pass
s1.add(123)
s1.add(123)
s1.add(123)
# print(s1)
# 输出{123}重复的元素被自动去掉
# 清除内容
# def clear(self, *args, **kwargs): # real signature unknown
# """ Remove all elements from this set. 清除内容"""
# pass
s1.clear()
# print(s1)
# 输出set() 空集合
# 浅拷贝
# def copy(self, *args, **kwargs): # real signature unknown
# """ Return a shallow copy of a set. 浅拷贝 """
# pass
# 不知道怎么用,先忘记吧!
# A中存在,B中不存在
# def difference(self, *args, **kwargs): # real signature unknown
# """
# Return the difference of two or more sets as a new set. A中存在,B中不存在
#
# (i.e. all elements that are in this set but not the others.)
# """
# pass
# 谁调用谁就是A,s1调用,那就是s1中存在,s2中不存在,结果就是11
# 那么,还可以反过来,s2调用,那就是s2中存在,s1中不存在,结果就是44
s1 = {11,22,33}
s2 = {22,33,44}
# s3 = s1.difference(s2)
# s4 = s2.difference(s1)
# print(s3)
# print(s4)
# 输出:
# {11}
# {44}
# 从当前集合中删除和B中相同的元素"""
# def difference_update(self, *args, **kwargs): # real signature unknown
# """ Remove all elements of another set from this set. 从当前集合中删除和B中相同的元素"""
# pass
# s1.difference_update(s2)
# print(s1)
# 输出
# {11}
# s1 = {11,22,33}
# s2 = {22,33,44}
# s1和s2相同的是22,33删除后剩下11,然后再把11赋值给s1,反之同理。
# s2.difference_update(s1)
# print(s2)
# 输出
# {44}
# difference和difference_update使用场景说明:
# 如果以后还会用到s1,那么就应该用difference
# 如果以后用不到s1,那么就可以用difference_update,这样做可以减少变量的声明,减少内存空间的占用。
#对称差集
# 把a中存在,B中不存在的,以及,B中存在,A中不存在的合起来,就是对称差集。结果就是{11, 44}
# def symmetric_difference(self, *args, **kwargs): # real signature unknown
# """
# Return the symmetric difference of two sets as a new set. 对称差集
#
# (i.e. all elements that are in exactly one of the sets.)
# """
# pass
s5 = s1.symmetric_difference(s2)
s6 = s2.symmetric_difference(s1)
# print(s5)
# 输出:
# {11, 44}
# print(s6)
# 输出:
# {11, 44}
# 由此看出s1 s2的对称差集,和s2 s1的对称差集是一样的
# print(s1)
# 输出:
# {33, 11, 22}
# print(s2)
# 输出:
# {33, 44, 22}
# 可以看出s1和s2都没有改变,还是原来s1和s2,symmetric_difference是把对称差集,赋值给新的变量,不会改变原来的s1和s2
# 对称差集,并更新到a中
# def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
# """ Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中 """
# pass
s1.symmetric_difference_update(s2)
# print(s1)
# 输出:
# {11, 44}
# 把a中存在,B中不存在的,以及,B中存在,A中不存在的合起来,再赋值给a
# symmetric_difference和symmetric_difference_update使用场景说明:
# 如果以后还会用到s1,那么就应该用symmetric_difference
# 如果以后用不到s1,那么就可以用symmetric_difference_update,这样做可以减少变量的声明,减少内存空间的占用。
# 移除指定元素,不存在不保错
# def discard(self, *args, **kwargs): # real signature unknown
# """
# Remove an element from a set if it is a member.
#
# If the element is not a member, do nothing. 移除指定元素,不存在不保错
# """
# pass
# s1 = {11,22,33,'alex'}
# s1.discard('alex')
# s1.discard(11)
# s1.discard(1111)
# print(s1)
# 是的,就是这个效果不报错
# 移除元素
# def pop(self, *args, **kwargs): # real signature unknown
# """
# Remove and return an arbitrary set element.
# Raises KeyError if the set is empty. 移除元素
# """
# pass
# s1 = {11,22,33,'alex'}
# s1.pop() #不加参数
# print(s1)
# 输出
# {'alex', 11, 22}
# 随机删除,集合是无序的,所以会删除哪一个,不知道,随机删。
ret = s1.pop()
# print(ret)
# 将随机删除的元素,赋值给ret,就知道随机删除了哪一个。
# 移除指定元素,不存在保错
# def remove(self, *args, **kwargs): # real signature unknown
# """
# Remove an element from a set; it must be a member.## If the element is not a member, raise a KeyError. 移除指定元素,不存在保错# """# passs1 = {11,22,33,'alex'}# s1.remove(11)# print(s1)# 输出# {33, 'alex', 22}# s1.remove(2222) #不存在,就报错。# Traceback (most recent call last):# File "C:/Users/Jam/PycharmProjects/s13/d3v1/0_set集合.py", line 208, in <module># s1.remove(2222)# KeyError: 2222# 交集# def intersection(self, *args, **kwargs): # real signature unknown# """# Return the intersection of two sets as a new set. 交集## (i.e. all elements that are in both sets.)# """# pass# s1 = {11,22,33}# s2 = {22,33,44}# s3 = s1.intersection(s2)# print(s3)# 输出# {33, 22}# def intersection_update(self, *args, **kwargs): # real signature unknown# """ Update a set with the intersection of itself and another. 取交集并更更新到A中 """# pass# s1 = {11,22,33}# s2 = {22,33,44}# s1.intersection_update(s2)# print(s1)# 输出# {33, 22}# 原理同上,带update的都是随调用就把值赋值给谁。# def isdisjoint(self, *args, **kwargs): # real signature unknown# """ Return True if two sets have a null intersection. 如果没有交集,返回True,否则返回False"""# pass# s1 = {11,22,33}# s2 = {22,33,44}# s3 = s1.isdisjoint(s2)# print(s3)# # 输出:因为有交集所以返回False# False# def issubset(self, *args, **kwargs): # real signature unknown# """ Report whether another set contains this set. 是否是子序列"""# pass# s1 = {22,33}# s2 = {22,33,44}# s3 = s1.issubset(s2)# print(s3)# 输出:s2里面包含s1,所以返回Ture# True# def issuperset(self, *args, **kwargs): # real signature unknown# """ Report whether this set contains another set. 是否是父序列"""# pass# s1 = {22,33}# s2 = {22,33,44}# s3 = s2.issuperset(s1)# print(s3)# # 输出:s2里面包含s1,所以s2是s1的序列。# True# def union(self, *args, **kwargs): # real signature unknown# """# Return the union of sets as a new set. 并集## (i.e. all elements that are in either set.)# """# pass# s1 = {11,22,33}# s2 = {22,33,44}# s3 = s1.union(s2)# print(s3)# 输出:称为并集或者合集,相同的只取一个# {33, 22, 11, 44}#批量更新,add是一个一个的添加,如果需要批量添加就需要用到update# def update(self, *args, **kwargs): # real signature unknown# """ Update a set with the union of itself and others. 更新 """# passs1 = set()s1.add(11)s1.add(22)s1.add(33)# print(s1)# 输出# {33, 11, 22}lis = [33,44,55,66] #列表s1.update(lis) #括号传入的参数,可以迭代的对象都可以,比如:字符串、列表、元组# print(s1)# 输出# {33, 66, 11, 44, 22, 55}# tup = (77,88,'hello') #元组# s1.update(tup)# print(s1)# 输出# {33, 66, 11, 44, 77, 22, 55, 88, 'hello'}# str = 'jamjam' #字符串# s1.update(str)# print(s1)# {33, 66, 11, 44, 'a', 22, 55, 'm', 'j'}# dic = {11:'hehe',22:'haha'} #不会报错,但看起来可迭代的对象不可以是字典# s1.update(dic) #update接收一个可以被迭代,被for循环的对象,然后调用add方法一个一个的加进来。# print(s1)# 输出# {33, 66, 11, 44, 22, 55}# print(type(s1)) #<class 'set'># dic1 = {11:"jam",22:"xixi"}# for i in dic1:# print(i)