zoukankan      html  css  js  c++  java
  • Python学习【第八篇】Set集合

    Set集合

    set集合是无序,不能重复,可嵌套的序列

    如何创建

    li = []
    dic = {"k1":123}
    se = {"123","456"}
    
    # 查看它的类型 
    print (type(se))
    
    # 输出
    <class 'set'>
    
    --------------------------------------------------------------
    # 创建: 
    
    s1 = {11,22}
    
    s2 = set()  # 创建一个空集合
    
    s3 = set([11,22,33,44])
    
    ---------------------------------------------------------------
    # set集合无序,且不重复的序列
    
    li = [11,22,11,33]
    s4 = set(li)
    print(s4)
    
    # 输出
    {33, 11, 22}

    功能

    ·添加元素

    s = set()
    s.add(123)
    print(s)
    
    # 输出
    {123}
    
    
    # PS:添加相同元素,不会生效,因为之前说过set是不重复的序列
    s.add(123)
    s.add(123)
    print(s)
    
    # 输出
    {123}

    ·清楚所有内容

    s.clear()
    print(s)
    
    # 输出
    set()

    ·浅拷贝

    s.add(456)
    s1 = s.copy()
    print(s1)
    
    # 输出
    {456}

    ·取不同

    s1 = {11,22,33}
    s2 = {22,33,44}
    
    ----------------------------------------------------------
    
    # s1中存在,s2中不存在
    s3 = s1.difference(s2)
    print(s3)
    
    
    # 输出
    {11}
    
    ----------------------------------------------------------
    
    # s1中存在,s2中不存在,并更新到s1
    s1.difference_update(s2)
    print(s1)
    
    
    # 输出
    {11}
    
    -----------------------------------------------------------
    
    # 取s1,s2中不同的元素
    s4 = s1.symmetric_difference(s2)
    print(s4)
    
    
    # 输出
    {11,44}
    
    ------------------------------------------------------------
    
    # 取s1,s2中不同的元素,并更新到s1
    s1.symmetric_difference_update(s2)
    print(s1)
    
    
    # 输出
    {11,44}

    ·移除

    #移除指定元素(不存在不报错)
    s1 = {11,22,33}
    s1.discard(11)
    print(s1)
    
    # 输出
    {33, 22}
    
    ---------------------------------------------------
    
    # 移除指定元素(但报错)
    s1.remove(1111)
    print(s1)
    
    ---------------------------------------------------
    
    # 随机移除
    s2 = {11,22,33}
    ret = s2.pop()
    print(ret)
    print(s2)
    
    # 输出
    33
    {11,22}
     
    
    ·交集
    
    s1 = {11,22,33}
    s2 = {22,33,44}
    s3 = s1.intersetion(s2)
    print(s3)
    
    # 输出
    {33, 22}
    
    -------------------------------------------
    
    # 计算出交集直接更新到s1
    s1.intersection_update(s2)
    print(s1)
    
    # 输出
    {33,22}

    ·并集

    s1 = {11,22,33}
    s2 = {22,33,44}
    s3 = s1.union(s2)
    print(s3)
    
    # 输出
    {33, 22, 11, 44}

    ·更新

    # 批量更新可以迭代的对象
    s1 = {11,22,33}
    li = [44,22,33,11,55]
    
    # 更新列表
    s1.update(li)
    print(s1)
    
    # 输出
    {33, 11, 44, 22, 55}
    
    -------------------------------------------------
    
    # 更新字符串
    s1 = {11,22,33}
    a = "boubon"
    s1.update(a)
    print(s1)
    
    # 输出
    {33, 'n', 11, 'o', 'u', 22, 'b'}
      1 class set(object):
      2     """
      3     set() -> new empty set object
      4     set(iterable) -> new set object
      5      
      6     Build an unordered collection of unique elements.
      7     """
      8     def add(self, *args, **kwargs): # real signature unknown
      9         """
     10         Add an element to a set,添加元素
     11          
     12         This has no effect if the element is already present.
     13         """
     14         pass
     15  
     16     def clear(self, *args, **kwargs): # real signature unknown
     17         """ Remove all elements from this set. 清除内容"""
     18         pass
     19  
     20     def copy(self, *args, **kwargs): # real signature unknown
     21         """ Return a shallow copy of a set. 浅拷贝  """
     22         pass
     23  
     24     def difference(self, *args, **kwargs): # real signature unknown
     25         """
     26         Return the difference of two or more sets as a new set. A中存在,B中不存在
     27          
     28         (i.e. all elements that are in this set but not the others.)
     29         """
     30         pass
     31  
     32     def difference_update(self, *args, **kwargs): # real signature unknown
     33         """ Remove all elements of another set from this set.  从当前集合中删除和B中相同的元素"""
     34         pass
     35  
     36     def discard(self, *args, **kwargs): # real signature unknown
     37         """
     38         Remove an element from a set if it is a member.
     39          
     40         If the element is not a member, do nothing. 移除指定元素,不存在不保错
     41         """
     42         pass
     43  
     44     def intersection(self, *args, **kwargs): # real signature unknown
     45         """
     46         Return the intersection of two sets as a new set. 交集
     47          
     48         (i.e. all elements that are in both sets.)
     49         """
     50         pass
     51  
     52     def intersection_update(self, *args, **kwargs): # real signature unknown
     53         """ Update a set with the intersection of itself and another.  取交集并更更新到A中 """
     54         pass
     55  
     56     def isdisjoint(self, *args, **kwargs): # real signature unknown
     57         """ Return True if two sets have a null intersection.  如果没有交集,返回True,否则返回False"""
     58         pass
     59  
     60     def issubset(self, *args, **kwargs): # real signature unknown
     61         """ Report whether another set contains this set.  是否是子序列"""
     62         pass
     63  
     64     def issuperset(self, *args, **kwargs): # real signature unknown
     65         """ Report whether this set contains another set. 是否是父序列"""
     66         pass
     67  
     68     def pop(self, *args, **kwargs): # real signature unknown
     69         """
     70         Remove and return an arbitrary set element.
     71         Raises KeyError if the set is empty. 移除元素
     72         """
     73         pass
     74  
     75     def remove(self, *args, **kwargs): # real signature unknown
     76         """
     77         Remove an element from a set; it must be a member.
     78          
     79         If the element is not a member, raise a KeyError. 移除指定元素,不存在保错
     80         """
     81         pass
     82  
     83     def symmetric_difference(self, *args, **kwargs): # real signature unknown
     84         """
     85         Return the symmetric difference of two sets as a new set.  对称差集
     86          
     87         (i.e. all elements that are in exactly one of the sets.)
     88         """
     89         pass
     90  
     91     def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
     92         """ Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中 """
     93         pass
     94  
     95     def union(self, *args, **kwargs): # real signature unknown
     96         """
     97         Return the union of sets as a new set.  并集
     98          
     99         (i.e. all elements that are in either set.)
    100         """
    101         pass
    102  
    103     def update(self, *args, **kwargs): # real signature unknown
    104         """ Update a set with the union of itself and others. 更新 """
    105         pass
    set(object)

    类CMDB的小实验

    old_dic = {
        "#1": 8,
        "#2": 4,
        "#4": 2,
    }
    
    new_dic = {
        "#1": 4,
        "#2": 4,
        "#3": 2,
    }
    
    '''
    应该删除哪几个槽位
        需求分析:
        1.old_dic存在,new_dic不存在的key
            old_keys = old_dic.keys()
            old_set = set(old_keys)
    
            new_keys = new_dic.keys()
            new_set = set(new_keys)
    
            old_set.differents(new_set) 
    应该更新哪几个槽位
    应该增加哪几个槽位
    '''
    
    old_keys = set(old_dic.keys())
    new_keys = set(new_dic.keys())
    
    remove_set = old_keys.difference(new_keys)
    add_set = new_keys.difference(old_keys)
    update_set = old_keys.intersection(new_keys)
  • 相关阅读:
    1、一条sql查询语句的执行过程
    go 内存分配
    GO Json
    gorm CRUD:读写数据
    go 基于切片的队列实现
    go的错误处理
    grpc
    sqlalchemy 判断字段是否存在
    定时函数
    用Python获取Linux资源信息的三种方法
  • 原文地址:https://www.cnblogs.com/Bourbon-tian/p/5938696.html
Copyright © 2011-2022 走看看