1. 程式人生 > >python3集合方法統計

python3集合方法統計

none 清空 nothing intersect subset false hid whether div

1、update()

  • 官方說明:
技術分享
    def update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the union of itself and others. """
        pass
View 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. """
        pass
View 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): #
real signature unknown """ Remove and return an arbitrary set element. Raises KeyError if the set is empty. """ pass
View Code

描述:隨機刪除集合中的一個元素

參數:

返回值:返回被刪除的元素

  • 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘}
s2 = s1.pop()    # 隨機刪除集合中的一個元素
print(type(s1),s1)  
print(type(s2),s2)

  輸出結果:

技術分享
<class set> {lisa, knight, william}
<class str> pudding
View Code

4、clear()

  • 官方說明:
技術分享
    def clear(self, *args, **kwargs): # real signature unknown
        """ Remove all elements from this set. """
        pass
View Code

描述:清空字典

參數:

返回值:None(原集合會被修改)

示例:

s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘}
s2 = s1.clear()  # 清空集合
print(type(s1),s1)
print(type(s2),s2)

  輸出結果:

技術分享
<class set> set()
<class NoneType> None
View 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.
        """
        pass
View 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> None
View 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.
        """
        pass
View 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> None
View 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.)
        """
        pass
View 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. """
        pass
View 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.
        """
        pass
View 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.)
        """
        pass
View 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. """
        pass
View 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. """
        pass
View 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> True
View Code

13、issubset()

  • 官方說明:
技術分享
    def issubset(self, *args, **kwargs): # real signature unknown
        """ Report whether another set contains this set. """
        pass
View 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> False
View Code

14、issuperset()

  • 官方說明:
技術分享
    def issuperset(self, *args, **kwargs): # real signature unknown
        """ Report whether this set contains another set. """
        pass
View 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> False
View 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.)
        """
        pass
View 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. """
        pass
View 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.)
        """
        pass
View 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集合方法統計