• Python基础(四)-集合


    数据类型总结:

    1、按照数据可变不可变:

    可变:列表、字典

    不可变:数字、字符串、元组

    x={'1':1}
    print(id(x))
    x.update({'2':2})
    print(x)
    print(id(x))
    
    #执行结果
    5584648
    {'2': 2, '1': 1}
    5584648
    View Code
    #列表亦同理
    def a(x=[]):
        x.append(1)
        print(id(x))
        return x
    print(a())
    print(a())
    
    #执行结果
    14045576
    [1]
    14045576
    [1, 1]
    View Code

    2、按照访问方式:

    直接访问:数字

    顺序访问:列表、元组、字符串

    映射:字典

    3、按照存放元素个数:

    容器类型:列表、元组、字典

    原子:数字、字符串

    bytes类型

    定义:存8bit整数,数据基于网络传输或内存变量存储到硬盘时需要转成bytes类型,字符串前置b代表为bytes类型

    集合:

    特点:无序;元素必须是不可变类型数据;不同元素(无重复元素)

    属于可变类

    集合创建:

    #创建可变集合
    a = set()
    
    b=set('hello')
    print(b) 
    # {'o', 'e', 'l', 'h'}
    
    #创建不可变集合
    nb = frozenset(b)
    print(nb)
    frozenset({'l', 'e', 'h', 'o'})

    常用方法:

    ##增

    1、add  #一次只能添加一个集合元素

    2、update  #增加可迭代数据,把每个元素迭代更新到集合,可以更新多个数据

    ##删

    1、remove()  #删除的元素必须是集合中的元素,否则会报错

    2、discard()  #删除的元素可以不是集合中的元素

    3、pop()  #随机删除一个元素

    4、clear()  #清空

    ##判断

    1、isdisjoint()

    判断两个集合是否有交集,没有则返回 True

    2、issubset()

    a.issubset(b) 判断a 是否是b的子集,是则返回 True

    3、issuperset()

    a.issuperset() 判断a是否是b的父集合,是则返回 True

    ##浅复制

    1、copy()

    ##运算

    1、交集 & intersection

    a.intersection(b)  #a和b相同元素

    2、并集 ^ union

    a.union(b)  #a和b组成新的集合

    3、差集 - difference

    a.difference(b)  #存在a中,但不存在b中

    4、symmetric_difference #对称差

    a.symmetric_difference(b)== (a-b)^(b-a)

    集合工厂函数:

    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
            """
            相当于s1-s2
            
            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
            """ Remove all elements of another set from this set. """
            pass
    
        def discard(self, *args, **kwargs): # real signature unknown
            """
            与remove功能相同,删除元素不存在时不会抛出异常
            
            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
            """
            相当于s1&s2
            
            Return the intersection of two sets as a new set.
            
            (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
            """ 
            相当于s1<=s2
            
            Report whether another set contains this set. """
            pass
    
        def issuperset(self, *args, **kwargs): # real signature unknown
            """
            相当于s1>=s2
            
             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
            """
            相当于s1^s2
            
            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
            """
            相当于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
    
        def __and__(self, *args, **kwargs): # real signature unknown
            """ Return self&value. """
            pass
    
        def __contains__(self, y): # real signature unknown; restored from __doc__
            """ x.__contains__(y) <==> y in x. """
            pass
    
        def __eq__(self, *args, **kwargs): # real signature unknown
            """ Return self==value. """
            pass
    
        def __getattribute__(self, *args, **kwargs): # real signature unknown
            """ Return getattr(self, name). """
            pass
    
        def __ge__(self, *args, **kwargs): # real signature unknown
            """ Return self>=value. """
            pass
    
        def __gt__(self, *args, **kwargs): # real signature unknown
            """ Return self>value. """
            pass
    
        def __iand__(self, *args, **kwargs): # real signature unknown
            """ Return self&=value. """
            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, *args, **kwargs): # real signature unknown
            """ Return self|=value. """
            pass
    
        def __isub__(self, *args, **kwargs): # real signature unknown
            """ Return self-=value. """
            pass
    
        def __iter__(self, *args, **kwargs): # real signature unknown
            """ Implement iter(self). """
            pass
    
        def __ixor__(self, *args, **kwargs): # real signature unknown
            """ Return self^=value. """
            pass
    
        def __len__(self, *args, **kwargs): # real signature unknown
            """ Return len(self). """
            pass
    
        def __le__(self, *args, **kwargs): # real signature unknown
            """ Return self<=value. """
            pass
    
        def __lt__(self, *args, **kwargs): # real signature unknown
            """ Return self<value. """
            pass
    
        @staticmethod # known case of __new__
        def __new__(*args, **kwargs): # real signature unknown
            """ Create and return a new object.  See help(type) for accurate signature. """
            pass
    
        def __ne__(self, *args, **kwargs): # real signature unknown
            """ Return self!=value. """
            pass
    
        def __or__(self, *args, **kwargs): # real signature unknown
            """ Return self|value. """
            pass
    
        def __rand__(self, *args, **kwargs): # real signature unknown
            """ Return value&self. """
            pass
    
        def __reduce__(self, *args, **kwargs): # real signature unknown
            """ Return state information for pickling. """
            pass
    
        def __repr__(self, *args, **kwargs): # real signature unknown
            """ Return repr(self). """
            pass
    
        def __ror__(self, *args, **kwargs): # real signature unknown
            """ Return value|self. """
            pass
    
        def __rsub__(self, *args, **kwargs): # real signature unknown
            """ Return value-self. """
            pass
    
        def __rxor__(self, *args, **kwargs): # real signature unknown
            """ Return value^self. """
            pass
    
        def __sizeof__(self): # real signature unknown; restored from __doc__
            """ S.__sizeof__() -> size of S in memory, in bytes """
            pass
    
        def __sub__(self, *args, **kwargs): # real signature unknown
            """ Return self-value. """
            pass
    
        def __xor__(self, *args, **kwargs): # real signature unknown
            """ Return self^value. """
            pass
    
        __hash__ = None
    
    查看
    
    查看
    set工厂函数

    练习:

     1 # 数据库中原有
     2 old_dict = {
     3     "#1":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 },
     4     "#2":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 }
     5     "#3":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 }
     6 }
     7    
     8 # cmdb 新汇报的数据
     9 new_dict = {
    10     "#1":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 800 },
    11     "#3":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 }
    12     "#4":{ 'hostname':c2, 'cpu_count': 2, 'mem_capicity': 80 }
    13 }
    14 
    15 #添加:旧数据不存在而新数据有的  new_set-olb_set
    16 #删除:新数据中没有,旧数据中存在的 old_set-new_set
    17 #更新:旧数据存在,新数据也存在而且内容变更  olb_set&new_set(且value不同)
  • 相关阅读:
    Easy | LeetCode 154 | 剑指 Offer 11. 旋转数组的最小数字 | 二分法
    Easy | 剑指 Offer 17. 打印从1到最大的n位数 | 大数
    Easy | LeetCode 191 | 剑指 Offer 15. 二进制中1的个数 | 位运算
    Easy | 剑指 Offer 57
    Medium | LeetCode 15. 三数之和 | 双指针法
    jq load()方法实现html 模块化。
    vue 剪切图片上传头像,使用 cropperjs 实现
    vue 的nextTick的理解
    使用nodejs进行开发,concurrently模块,使我们同时执行多个命令。
    cookie生命周期expires 时间过期,但是cookie没有被浏览器清理的问题
  • 原文地址:https://www.cnblogs.com/chenzhuo-/p/6116650.html
Copyright © 2020-2023  润新知