集合:python中的一個基本數據類型 set集合中的元素是不能夠重複的,無序的,裏面的元素必須是可hash,不可變的數據類型(int,int,bool,tuple) set集合就是不保存值的字典,如{'張三','李四'} set集合自己就是可變的數據類型
#擴展:可變和不可變數據類型python
#可變數據類型:當該數據類型的對應變量的值發生了改變,那麼它對應的內存地址不發生改變 #例子: 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+鼠標左鍵點setapp
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的,不可變的數據類型ide
#例子:若是使用可變數據類型,不可哈希的元素就會報錯
#提示:不可變的數據類型: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'}} #報錯,有集合
#集合的經常使用例子(去重)ui
#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]
#注意點1:集合中的元素是不重複的,且無序的,因此打印出來的元素位置可能會和定義的位置不同this
#注意點2:set集合是自己可變的數據類型,因此能夠直接在原來的對象上進行操做spa
#2.1.增長3d
#關鍵字 #1.add #2.update(迭代更新)
#例子:rest
s = {"蔣小魚","魯炎","張衝"} s.add("龍大隊") print(s) #{'蔣小魚', '龍大隊', '魯炎', '張衝'} s.add("龍大隊") #若是重複的內容是不會被添加進去的 print(s) #{'蔣小魚', '龍大隊', '魯炎', '張衝'} s = {"蔣小魚","魯炎","張衝"} s.update("武黑臉") #迭代更新,一個字一個字添加 print(s) #{'蔣小魚', '魯炎', '臉', '張衝', '武', '黑'}
#2.2.刪除code
#關鍵字: #1.pop() #隨機刪除 #2.remove(n) #指定元素刪除 #3.clear() #清空
#例子:orm
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)
經常使用操做有:交集,並集,差集,反交集,子集,超集
#例子:
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