python3集合方法統計
1、update()
- 官方說明:
def update(self, *args, **kwargs): # real signature unknown """ Update a set with the union of itself and others. """ passView Code
描述:擴展集合
參數:要添加的集合
返回值:None(原集合會被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘sky‘} s3 = s1.update(s2) # 擴展集合s1 print(type(s3),s3) # 查看返回值 print(type(s1),s1) # 打印擴展後的集合s1
輸出結果:
<class ‘NoneType‘> None <class ‘set‘> {‘knight‘, ‘sky‘, ‘pudding‘, ‘lisa‘, ‘william‘}View Code
2、copy()
- 官方說明:
def copy(self, *args, **kwargs): # real signature unknown """ Return a shallow copy of a set. """ passView Code
描述:復制集合
參數:無
返回值:返回一個和原集合一樣的新的集合
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = s1.copy() # 對集合s1進行復制 print(type(s1),s1) print(type(s2),s2)
輸出結果:
<class ‘set‘> {‘knight‘, ‘pudding‘, ‘william‘, ‘lisa‘} <class ‘set‘> {‘knight‘, ‘pudding‘, ‘william‘, ‘lisa‘}View Code
3、pop()
- 官方說明:
def pop(self, *args, **kwargs): #View Codereal signature unknown """ Remove and return an arbitrary set element. Raises KeyError if the set is empty. """ pass
描述:隨機刪除集合中的一個元素
參數:無
返回值:返回被刪除的元素
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = s1.pop() # 隨機刪除集合中的一個元素 print(type(s1),s1) print(type(s2),s2)
輸出結果:
<class ‘set‘> {‘lisa‘, ‘knight‘, ‘william‘} <class ‘str‘> puddingView Code
4、clear()
- 官方說明:
def clear(self, *args, **kwargs): # real signature unknown """ Remove all elements from this set. """ passView Code
描述:清空字典
參數:無
返回值:None(原集合會被修改)
示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = s1.clear() # 清空集合 print(type(s1),s1) print(type(s2),s2)
輸出結果:
<class ‘set‘> set() <class ‘NoneType‘> NoneView Code
5、remove()
- 官方說明:
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. """ passView Code
描述:刪除集合中指定的元素
參數:element 元素
返回值:None(原集合會被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = s1.remove(‘lisa‘) print(type(s1),s1) print(type(s2),s2) # 返回值為空
輸出結果:
<class ‘set‘> {‘william‘, ‘pudding‘, ‘knight‘} <class ‘NoneType‘> NoneView Code
6、add()
- 官方說明:
def add(self, *args, **kwargs): # real signature unknown """ Add an element to a set. This has no effect if the element is already present. """ passView Code
描述:為集合增加元素
參數:element 元素
返回值:None(原集合會被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = s1.add(‘sky‘) print(type(s1),s1) print(type(s2),s2) # 返回值為空
輸出結果:
<class ‘set‘> {‘pudding‘, ‘lisa‘, ‘william‘, ‘knight‘, ‘sky‘} <class ‘NoneType‘> NoneView Code
7、difference()
- 官方說明:
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.) """ passView Code
描述:差集運算,原集合不更新
參數:set 要對比的集合
返回值:得到一個差集
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘sky‘,‘william‘,‘hello‘,‘knight‘} s3 = s1.difference(s2) print(type(s3),s3) # 得到一個差集。 print(type(s1),s1) # 原集合不更新
輸出結果:
<class ‘set‘> {‘lisa‘, ‘pudding‘} <class ‘set‘> {‘pudding‘, ‘lisa‘, ‘knight‘, ‘william‘}View Code
8、difference_update
- 官方說明:
def difference_update(self, *args, **kwargs): # real signature unknown """ Remove all elements of another set from this set. """ passView Code
描述:差集運算,原集合更新
參數:set 要對比的集合
返回值:None(原集合會被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘sky‘,‘william‘,‘hello‘,‘knight‘} s3 = s1.difference_update(s2) print(type(s3),s3) # 返回None print(type(s1),s1) # 原集被更新
輸出結果:
<class ‘NoneType‘> None <class ‘set‘> {‘pudding‘, ‘lisa‘}View Code
9、discard()
- 官方說明:
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. """ passView Code
描述:刪除集合中指定的元素
參數:element 元素
返回值:None(原集合會被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = s1.discard(‘william‘) print(type(s2),s2) print(type(s1),s1)
輸出結果:
<class ‘NoneType‘> None <class ‘set‘> {‘lisa‘, ‘knight‘, ‘pudding‘}View Code
10、intersection()
- 官方說明:
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.) """ passView Code
描述:交集運算,原集合不更新
參數:set 要對比的集合
返回值:得到一個交集
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘william‘,‘xxxx‘} s3 = s1.intersection(s2) print(type(s3),s3) # 得到一個交集 print(type(s1),s1) # 原集合不更新
輸出結果:
<class ‘set‘> {‘william‘} <class ‘set‘> {‘william‘, ‘lisa‘, ‘knight‘, ‘pudding‘}View Code
11、intersection_update()
- 官方說明:
def intersection_update(self, *args, **kwargs): # real signature unknown """ Update a set with the intersection of itself and another. """ passView Code
描述:交集運算,原集合更新
參數:set 要對比的集合
返回值:None(原集合會被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘william‘,‘xxxx‘} s3 = s1.intersection_update(s2) print(type(s3),s3) # 返回None print(type(s1),s1) # 原集合更新
輸出集合:
<class ‘NoneType‘> None <class ‘set‘> {‘william‘}View Code
12、isdisjoint()
- 官方說明:
def isdisjoint(self, *args, **kwargs): # real signature unknown """ Return True if two sets have a null intersection. """ passView Code
描述:判斷是否有交集,如果有交集則返回False,沒有返回True
參數:set 要對比的集合
返回值:返回一個布爾值
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘xxxx‘,‘lisa‘} s3 = s1.isdisjoint(s2) # 有交集則返回False print(type(s3),s3) #-------------------------------------------- s4 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s5 = {‘xxxx‘,‘yyyyy‘,‘kkkkkk‘,‘uuuuuu‘} s6 = s4.isdisjoint(s5) # 沒有交集則返回True print(type(s6),s6)
輸出結果:
<class ‘bool‘> False <class ‘bool‘> TrueView Code
13、issubset()
- 官方說明:
def issubset(self, *args, **kwargs): # real signature unknown """ Report whether another set contains this set. """ passView Code
描述:判斷原集合是否是要對比的集合的子集,如果是則返回True,否則返回False
參數:set 要對比的集合
返回值:得到一個布爾值
- 示例:
s1 = {‘william‘,‘pudding‘} s2 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s3 = s1.issubset(s2) # s1是否是s2的子集,所以返回True print(type(s3),s3) #-------------------------------------- s4 = {‘william‘,‘xxxxx‘} s5 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s6 = s4.issubset(s5) # s4不是s5的子集,所以返回False print(type(s6),s6)
輸出結果:
<class ‘bool‘> True <class ‘bool‘> FalseView Code
14、issuperset()
- 官方說明:
def issuperset(self, *args, **kwargs): # real signature unknown """ Report whether this set contains another set. """ passView Code
描述:判斷原集合是否是要對比的集合的超(父)集,如果是則返回True,否則返回False
參數:set 要對比的集合
返回值:得到一個布爾值
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘william‘,‘pudding‘} s3 = s1.issuperset(s2) # s1是s2的超(父)集,所以返回True print(type(s3),s3) #-------------------------------------- s4 = {‘william‘,‘xxxxx‘} s5 = {‘william‘,‘lisa‘,‘nnnnn‘,‘xxxx‘} s6 = s4.issuperset(s5) # s4不是s5的超(父)集,所以返回True print(type(s6),s6)
輸出結果:
<class ‘bool‘> True <class ‘bool‘> FalseView Code
15、symmetric_difference()
- 官方說明:
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.) """ passView Code
描述:對稱差集運算,原集合不更新
參數:set 要對比的集合
返回值:返回一個對稱差集
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘william‘,‘pudding‘,‘xxxxxx‘} s3 = s1.symmetric_difference(s2) print(type(s3),s3) # 得到一個對稱差集 print(type(s1),s1) # 對稱差集運算,原集合不更新
輸出結果:
<class ‘set‘> {‘knight‘, ‘lisa‘, ‘xxxxxx‘} <class ‘set‘> {‘knight‘, ‘william‘, ‘lisa‘, ‘pudding‘}View Code
16、symmetric_difference_update()
- 官方說明:
def symmetric_difference_update(self, *args, **kwargs): # real signature unknown """ Update a set with the symmetric difference of itself and another. """ passView Code
描述:對稱差集運算,原集合更新
參數:set 要對比的集合
返回值:None(原集合會被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘william‘,‘pudding‘,‘xxxxxx‘} s3 = s1.symmetric_difference_update(s2) print(type(s3),s3) # 返回None print(type(s1),s1) # 對稱差集運算,原集合更新
輸出結果:
<class ‘NoneType‘> None <class ‘set‘> {‘lisa‘, ‘knight‘, ‘xxxxxx‘}View Code
17、union()
- 官方說明:
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.) """ passView Code
描述:並集運算,原集合不更新
參數:set 要對比的集合
返回值:得到一個新的集合,兩個集合中都有的元素都合並成一個。
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘william‘,‘pudding‘,‘xxxxxx‘} s3 = s1.union(s2) print(type(s3),s3) # 返回得到一個新的集合,兩個集合中都有的元素都合並成一個。 print(type(s1),s1) # 並集運算,原集合不更新
輸出結果:
<class ‘set‘> {‘xxxxxx‘, ‘knight‘, ‘william‘, ‘lisa‘, ‘pudding‘} <class ‘set‘> {‘pudding‘, ‘lisa‘, ‘knight‘, ‘william‘}View Code
python3集合方法統計