首页 > 代码库 > python3集合方法统计

python3集合方法统计

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集合方法统计