zoukankan      html  css  js  c++  java
  • 集合的操作

    # 集合操作
    # 集合是一个无序的,不重复的数据组合,它的主要作用如下:
    
    # 去重,把一个列表变成集合,就自动去重了
    # 关系测试,测试两组数据之前的交集、差集、并集等关系
    def nl():
            print('='.ljust(30, '='))
    
    list1 = [1, 2, 3, 4, 5, 6, 7, 8]
    list2 = [3, 4, 5, 6, 8, 0, 11, 23, 55]
    listNew = []
    
    '''
    # for i in list1:
    #     listNew = list1.copy()
    # print(listNew)    
    # for i in list2:
    #     if i not in listNew:
    #         listNew.append(i)
    # print(listNew)
    '''
    '''
    list3 = list1 + list2
    print(list3)
    list4 = set(list3)
    listNew = list(list4)
    print(listNew)
    '''
    '''
    for i in list1:
        if i in list2:
            print(i)
    '''
    list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
    print(type(list_1))
    list_1 = set(list_1)
    list_2 = set([2, 6, 0, 66, 22, 8, 4])
    print(list_1, list_2)
    
    '''
    关系测试:
    ----------------------------------------
    '''
    #交集   (1和2 都有)
    print('交集',list_1.intersection(list_2))
    
    #并集   (1和2 加起来)
    print('并集',list_1.union(list_2))
    
    #差集 (只存在1不存在2)
    print(list_1.difference(list_2))
    
    #子集   (1是2 的子集吗?)   False
    print(list_1.issubset(list_2))
    
    #父集   1是2 的父集吗   False
    print(list_1.issuperset(list_2))
    
    #对称差集       对称差集(项在t或s中,但不会同时出现在二者中)
    # 1 中独有和 2中独有的放在一起
    # 其实就是并集中去掉交集的
    print(list_1.symmetric_difference(list_2))
    
    #如果两个集合的交集为空,则返回True
    # 判断两个集合是否有交集        
    '''
    没有返回true
    有返回False
    '''
    print(list_1.isdisjoint(list_2))
    
    '''
    运算符方式
    '''
    a = set([3, 4, 5, 'l'])
    b = set('hello')
    
    print(a | b)    #并
    print(a & b)    #交
    print(a - b)    #差
    print(a ^ b)    #对称差
    
    nl()
    # 基本操作
    #增     单
    list_1.add(999)
    print(list_1)
    #增     多
    list_1.update([3, 77, 34])
    print(list_1)
    
    #删除一项
    list_1.remove(1)
    print(list_1)
    nl()
    
    #长度
    print(len(list_1))
    nl()
    
    # 测试 x 是否是 s 的成员
    x = 1
    s = set([1, 2, 3])
    result = x in list_1
    print(result)
    #==============================
    '''
    # 测试 x 是否不是 s 的成员
    x not in s  
    
    # 测试是否 s 中的每一个元素都在 t 中 
    s <= t
    
    # 测试是否 t 中的每一个元素都在 s 中
    s >= t
    '''
    #==============================
    '''
    s.union(t)  
    s | t  
    返回一个新的 set 包含 s 和 t 中的每一个元素 
    '''
    #==============================
    '''
    s.intersection(t)  
    s & t  
    返回一个新的 set 包含 s 和 t 中的公共元素  
      
    s.difference(t)  
    s - t  
    返回一个新的 set 包含 s 中有但是 t 中没有的元素  
      
    s.symmetric_difference(t)  
    s ^ t  
    返回一个新的 set 包含 s 和 t 中不重复的元素  
      
    s.copy()  
    返回 set “s”的一个浅复制  
    '''
    
    #删除任意一个元素并返回
    aa = set([1, 2, 3, 4, 5, 'a', 'b', 'c'])
    ret = aa.pop()
    print(ret)
    print(aa)
    
    #从集合中删除元素(如果它是成员)。
    # 如果元素不是成员,则什么也不做。
    print(aa.discard('a'))          #存在 'a' 所以删除了'a'
    print(aa)
    print(aa.discard(6))            # 6不存在所以返回none
    
    nl()
    

      

  • 相关阅读:
    RE
    【LeetCode】198. House Robber
    【LeetCode】053. Maximum Subarray
    【LeetCode】152. Maximum Product Subarray
    【LeetCode】238.Product of Array Except Self
    【LeetCode】042 Trapping Rain Water
    【LeetCode】011 Container With Most Water
    【LeetCode】004. Median of Two Sorted Arrays
    【LeetCode】454 4Sum II
    【LeetCode】259 3Sum Smaller
  • 原文地址:https://www.cnblogs.com/lcxiao/p/11386516.html
Copyright © 2011-2022 走看看