• python 学习_集合


    set
    特点:
    集合 是一个无序的, 不重复的数据组合,它的主要作用有:
       去重 把一个列表变成集合, 就自动去重了
    集合中的元素必须是不可变元素


    创建集合
    s = {1,2,3,4,5,3,5,1}
    print(s)
    print(type(s))
    
    #   {1, 2, 3, 4, 5}
    #   <class 'set'>
    
    
    
     
    交集
    s = {1,2,3,4,5,3,5,1}
    ss = {2,3,4}
    
    
    # &
    # intersection()
    
    print("s与ss的交集:     ",s.intersection(ss))
    print("s与ss的交集:     ",s&ss)
    
    # s与ss的交集:      {2, 3, 4}
    # s与ss的交集:      {2, 3, 4}



    并集

    s = {1,2,3,4,5,3,5,1}
    ss = {2,3,4}
    
    # 并集  等于2个列表去重
    
    #
    union # |
    print("s与ss的并集: ",s.union(ss)) print("s与ss的并集: ", s|ss )
    # s与ss的并集: {1, 2, 3, 4, 5} # s与ss的并集: {1, 2, 3, 4, 5}
    
    
    


    差集

    s = {1,2,3,4,5,3,5,1}
    ss = {2,3,4}
    # difference
    #-
    print("s与ss的差集: ",s.difference(ss))
    print("s与ss的差集: ",s-ss)
    # s与ss的差集:  {1, 5}
    # s与ss的差集:  {1, 5}


    对称差集

    s = {1,2,3,4,5,3,5,1}
    ss = {2,3,4,333}
    
    #symmetric_difference
    #^
    print(s.symmetric_difference(ss))
    print(s^ss)
    
    # {1, 5, 333}
    # {1, 5, 333}
     
    issubset
    # #issubset
    s = {1,2,3,4,5,3,5,1,333}
    ss = {2,3,4,333}
    
    # ss是s的子集
    print(ss.issubset(s))
    # True
    
    # s 是ss的子集
    print(s.issubset(ss))
    # False
    
    
    
     
    issuperset
    
    
    # #issuperset
    s = {1,2,3,4,5,3,5,1,333}
    ss = {2,3,4,333}
    
    # s 是ss的父集
    print(s.issuperset(ss))
    # True

    # ss是 s的父集 print(ss.issuperset(s)) # False
    
    
    



    元素的添加
    a = {1,2}
    a.update([1,34,5,6,67])  
    # update 添加多个元素
    
    a.add("wwww")
    # add  添加一个元素
    print(a)
    
    
    



    元素的删除
    discard    存在就删除, 不存在也不报错
    s = {1,2,3,4,5,3,5,1,333}
    # # 元素的删除
    # discard   存在就删除, 不存在 也不报错
    s.discard('2ww')
    print(s)
    # {1, 2, 3, 4, 5, 333}
    
    
    

    remove
    s = {1,2,3,4,5,3,5,1,333}
    s.remove('x')   # 不存在会抛出 KeyError错误
    
    
    
    
    
    Traceback (most recent call last):
      File "D:/python/luffycity/元组/集合类型.py", line 135, in <module>
        s.remove('x')   # 不存在会抛出 KeyError错误
    KeyError: 'x'
    
    
    


    元素的源码

    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.
             相当于 s1 - s2   差集  
            (i.e. all elements that are in this set but not the others.)
            """
            pass
    
        def difference_update(self, *args, **kwargs): # real signature unknown
            """ 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.
            
            与remove功能相同,删除元素不存在时不会抛出异常
            
            If the element is not a member, do nothing.
            """
            pass
    
        def intersection(self, *args, **kwargs): # real signature unknown
            """
            Return the intersection of two sets as a new set.
            
            相当于 s1&s2  求交集
            
            (i.e. all elements that are in both sets.)
            """
            pass
    
        def intersection_update(self, *args, **kwargs): # real signature unknown
            """ Update a set with the intersection of itself and another. """
            pass
    
        def isdisjoint(self, *args, **kwargs): # real signature unknown
            """ 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. 
            
            相当于 s1 <=s2
            """
            pass
    
        def issuperset(self, *args, **kwargs): # real signature unknown
            """ Report whether this set contains another set. 
            相当于 s1 >=s2
            
            """
            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.
            
            相当于 s1^s2      对称差集
            
            (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
            """
            
            并集   相当于s1|s2 
            
            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
    
    
    









     
  • 相关阅读:
    ASP.NET MVC布局
    C#微信扫码支付Demo
    ASP.NET MVC用户登录(Memcache存储用户登录信息)
    Memcached分布式缓存快速入门
    Log4Net日志配置
    ASP.NET MVC自定义异常处理
    Spring.Net快速入门:控制翻转、依赖注入、面向切面编程
    C#微信公众号开发入门教程
    APS.NET MVC4生成解析二维码简单Demo
    Entity Framwork学习笔记
  • 原文地址:https://www.cnblogs.com/augustyang/p/10438839.html
Copyright © 2020-2023  润新知