python入门到放弃(八)-基本数据类型之set集合
1.概述
集合:python中的一个基本数据类型 set集合中的元素是不可以重复的,无序的,里面的元素必须是可hash,不可变的数据类型(int,int,bool,tuple) set集合就是不保存值的字典,如{'张三','李四'} set集合本身就是可变的数据类型
#扩展:可变和不可变数据类型
#可变数据类型:当该数据类型的对应变量的值发生了改变,那么它对应的内存地址不发生改变 #例子: lst1 = [1,2,3,True] print(id(lst1)) lst1.append(False) print(id(lst1)) # 1695428338184 # 1695428338184 #不可变的数据类型:当数据类型对应的变量的值发生改变,变量的内存地址也发生了改变 #例子: a = 1 print(id(a),type(a)) a = 2 print(id(a),type(a)) #140704654406480 <class 'int'> # 140704654406512 <class 'int'>
#先来看看set集合的源码写了什么,方法:按ctrl+鼠标左键点set
class set(object): """ set() -> new empty set object set(iterable) -> new set object Build an unordered collection of unique elements. """ 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 def clear(self, *args, **kwargs): # real signature unknown """ Remove all elements from this set. """ pass def copy(self, *args, **kwargs): # real signature unknown """ Return a shallow copy of a set. """ pass def difference(self, *args, **kwargs): # real signature unknown """ Return the difference of two or more sets as a new set. (i.e. all elements that are in this set but not the others.) """ pass def difference_update(self, *args, **kwargs): # real signature unknown """ 删除当前set中的所有包含在 new set 里的元素 """ """ Remove all elements of another set from this set. """ pass 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 def intersection(self, *args, **kwargs): # real signature unknown """ 取交集,新创建一个set """ """ Return the intersection of two or more sets as a new set. (i.e. elements that are common to all of the sets.) """ pass def intersection_update(self, *args, **kwargs): # real signature unknown """ 取交集,修改原来set """ """ Update a set with the intersection of itself and another. """ pass def isdisjoint(self, *args, **kwargs): # real signature unknown """ 如果没有交集,返回true """ """ Return True if two sets have a null intersection. """ pass def issubset(self, *args, **kwargs): # real signature unknown """ 是否是子集 """ """ Report whether another set contains this set. """ pass def issuperset(self, *args, **kwargs): # real signature unknown """ 是否是父集 """ """ Report whether this set contains another set. """ pass def pop(self, *args, **kwargs): # real signature unknown """ 移除 """ """ Remove and return an arbitrary set element. Raises KeyError if the set is empty. """ pass 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. """ pass 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 def symmetric_difference_update(self, *args, **kwargs): # real signature unknown """ 差集,改变原来 """ """ Update a set with the symmetric difference of itself and another. """ pass 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 def update(self, *args, **kwargs): # real signature unknown """ 更新 """ """ Update a set with the union of itself and others. """ pass def __and__(self, y): # real signature unknown; restored from __doc__ """ x.__and__(y) <==> x&y """ pass def __cmp__(self, y): # real signature unknown; restored from __doc__ """ x.__cmp__(y) <==> cmp(x,y) """ pass def __contains__(self, y): # real signature unknown; restored from __doc__ """ x.__contains__(y) <==> y in x. """ pass def __eq__(self, y): # real signature unknown; restored from __doc__ """ x.__eq__(y) <==> x==y """ pass def __getattribute__(self, name): # real signature unknown; restored from __doc__ """ x.__getattribute__('name') <==> x.name """ pass def __ge__(self, y): # real signature unknown; restored from __doc__ """ x.__ge__(y) <==> x>=y """ pass def __gt__(self, y): # real signature unknown; restored from __doc__ """ x.__gt__(y) <==> x>y """ pass def __iand__(self, y): # real signature unknown; restored from __doc__ """ x.__iand__(y) <==> x&=y """ pass def __init__(self, seq=()): # known special case of set.__init__ """ set() -> new empty set object set(iterable) -> new set object Build an unordered collection of unique elements. # (copied from class doc) """ pass def __ior__(self, y): # real signature unknown; restored from __doc__ """ x.__ior__(y) <==> x|=y """ pass def __isub__(self, y): # real signature unknown; restored from __doc__ """ x.__isub__(y) <==> x-=y """ pass def __iter__(self): # real signature unknown; restored from __doc__ """ x.__iter__() <==> iter(x) """ pass def __ixor__(self, y): # real signature unknown; restored from __doc__ """ x.__ixor__(y) <==> x^=y """ pass def __len__(self): # real signature unknown; restored from __doc__ """ x.__len__() <==> len(x) """ pass def __le__(self, y): # real signature unknown; restored from __doc__ """ x.__le__(y) <==> x<=y """ pass def __lt__(self, y): # real signature unknown; restored from __doc__ """ x.__lt__(y) <==> x<y """ pass @staticmethod # known case of __new__ def __new__(S, *more): # real signature unknown; restored from __doc__ """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass def __ne__(self, y): # real signature unknown; restored from __doc__ """ x.__ne__(y) <==> x!=y """ pass def __or__(self, y): # real signature unknown; restored from __doc__ """ x.__or__(y) <==> x|y """ pass def __rand__(self, y): # real signature unknown; restored from __doc__ """ x.__rand__(y) <==> y&x """ pass def __reduce__(self, *args, **kwargs): # real signature unknown """ Return state information for pickling. """ pass def __repr__(self): # real signature unknown; restored from __doc__ """ x.__repr__() <==> repr(x) """ pass def __ror__(self, y): # real signature unknown; restored from __doc__ """ x.__ror__(y) <==> y|x """ pass def __rsub__(self, y): # real signature unknown; restored from __doc__ """ x.__rsub__(y) <==> y-x """ pass def __rxor__(self, y): # real signature unknown; restored from __doc__ """ x.__rxor__(y) <==> y^x """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ S.__sizeof__() -> size of S in memory, in bytes """ pass def __sub__(self, y): # real signature unknown; restored from __doc__ """ x.__sub__(y) <==> x-y """ pass def __xor__(self, y): # real signature unknown; restored from __doc__ """ x.__xor__(y) <==> x^y """ pass __hash__ = None set
#集合里面的元素必须可hash的,不可变的数据类型
#例子:如果使用可变数据类型,不可哈希的元素就会报错
#提示:不可变的数据类型:int,bool,str,tuple,可变的数据类型有:list,dict,set # set1 = {'1','sir',2,True,[1,2,3]} #报错,有列表, # set2 = {'1','sir',2,{"a":2}} #报错,有字典 # set3 = {'1','sir',{'1','sir'}} #报错,有集合
#集合的常用例子(去重)
#set集合中的元素是不重复的,且无序的 a = {"蒋小雨","鲁炎","张冲","鲁炎"} print(a) #{'蒋小雨', '张冲', '鲁炎'}:会去掉重复的,且不按顺序排 #####set集合最常用的就是去重### a = [1,3,4,4,3,2,3,4,5,5,5,45,5,254,54] s = set(a) #把列表转换成集合,进行去重复 lst = list(s) #把集合转换回列表 print(lst) #[1, 2, 3, 4, 5, 45, 54, 254] # 或者 print(list(set(a))) #[1, 2, 3, 4, 5, 45, 54, 254]
2.集合的增删改查
#注意点1:集合中的元素是不重复的,且无序的,所以打印出来的元素位置可能会和定义的位置不一样
#注意点2:set集合是本身可变的数据类型,所以可以直接在原来的对象上进行操作
#2.1.增加
#关键字 #1.add #2.update(迭代更新)
#例子:
s = {"蒋小鱼","鲁炎","张冲"} s.add("龙大队") print(s) #{'蒋小鱼', '龙大队', '鲁炎', '张冲'} s.add("龙大队") #如果重复的内容是不会被添加进去的 print(s) #{'蒋小鱼', '龙大队', '鲁炎', '张冲'} s = {"蒋小鱼","鲁炎","张冲"} s.update("武黑脸") #迭代更新,一个字一个字添加 print(s) #{'蒋小鱼', '鲁炎', '脸', '张冲', '武', '黑'}
#2.2.删除
#关键字: #1.pop() #随机删除 #2.remove(n) #指定元素删除 #3.clear() #清空
#例子:
s = {"蒋小鱼","鲁炎","张冲","阿甘"} item = s.pop() #随机删除 print(s) #{'张冲', '鲁炎', '阿甘'} print(item) #蒋小鱼 :查看到被删除的是蒋小鱼 s.remove("张冲") #直接删除指定的元素 # s.remove("黑脸") #如果删除不存在的元素就会报错 print(s) #{'蒋小鱼', '鲁炎', '阿甘'} s.clear() #情况set集合,需要注意的是set集合如果是空的,打印出来的是set(),因为要和dict区分的 print(s) #set()
#2.3.修改
#set集合中的数据没有索引,因为是无序的,所有没有办法去定位一个元素,所以没办法直接修改 # 但是可以采用先删除后添加的方式来完成修改操作
#例子:
s = {"蒋小鱼","鲁炎","张冲","阿甘"} s.remove("鲁炎") s.add("龙大队") print(s) #{'阿甘', '蒋小鱼', '龙大队', '张冲'}
#2.4.查询
#set是一个可迭代对象,所以可以进行for循环 s = {"蒋小鱼","鲁炎","张冲","阿甘"} for el in s: print(el)
3.集合的常用操作
常用操作有:交集,并集,差集,反交集,子集,超集
#例子:
a1 = {"蒋小鱼","鲁炎","张冲","阿甘"} a2 = {"刘能","赵四","张冲","谢大脚"}
#3.1.交集:就是两个集合中的共有元素
print(a1 & a2) #{'张冲'} #或者 print(a1.intersection(a2)) #{'张冲'}
#3.2.并集:蒋两个集合合并起来
print(a1 | a2) #{'张冲', '鲁炎', '阿甘', '赵四', '刘能', '蒋小鱼', '谢大脚'} print(a1.union(a2)) #{'张冲', '鲁炎', '阿甘', '赵四', '刘能', '蒋小鱼', '谢大脚'}
#3.3.差集:得到某一个中单独存在的
print(a1 - a2) #{'鲁炎', '阿甘', '蒋小鱼'} :得到第一个中单独存在的,如果是a2-a1,那么得到的是第二个中单独存在的 print(a1.difference(a2)) #{'蒋小鱼', '阿甘', '鲁炎'}
#3.4.反交集:得到两个集合中单独存在的数据,就是两个不一样的
print(a1 ^ a2) # #{'谢大脚', '蒋小鱼', '赵四', '刘能', '鲁炎', '阿甘'} print(a1.symmetric_difference(a2))
#3.5.子集: 谁是谁的子集,简单说就是你有的别人也有,别人有的你没有
a1 = {"蒋小鱼","张冲"} a2 = {"蒋小鱼","张冲","谢大脚"} print(a1 < a2) #a1是a2的子集吗? #True print(a1.issubset(a2)) #True
#3.6.超集:如果一个集合a1中的每一个元素都在集合a2中,且集合a2中可能包含a1中没有的元素,则集合a2就是a1的一个超集。
a1 = {"蒋小鱼","张冲"} a2 = {"蒋小鱼","张冲","谢大脚"} print(a1 > a2) #False #a1是a2的超集吗 print(a1.issuperset(a2)) #Flase