zoukankan      html  css  js  c++  java
  • 集合-set

    集合(set)是一个无序的不重复元素序列。

    可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。 

    下面列出一些常见的操作:

    """
    set.add(x) 将元素x添加到集合里
    """
    s = {1, 2, 3, 4, 5, }
    s.add('5')
    print(s)    # {1, 2, 3, 4, 5, '5'}
    
    """
    et.update(x) 将x添加到集合中,且参数可以是列表、元组、字典等
    """
    s = {'a', 'cc', 'f'}
    _list = ['w', 'a', 1]
    _dict = {'name': 'bb', 'age': 'cc', 'f': 11}
    s.update(_dict)
    print(s)          # {'a', 'f', 'age', 'cc', 'name'}
    s.update(_list)
    print(s)          # {'a', 'f', 1, 'age', 'cc', 'w', 'name'}
    
    """
    set.remove(x) 移除集合中元素,如果移除的元素不在集合中将发生错误
    """
    s = {'a', 'cc', 'f'}
    s.remove('cc')
    print(s)     # {'a', 'f'}
    
    """
    set.discard(x) 移除集合中元素,如果移除的元素不在集合中不会发生错误
    """
    s = {'a', 'cc', 'f'}
    s.discard('mm')  # 不会报错
    print(s)         # {'cc', 'f', 'a'}
    
    """
    set.pop() 随机删除集合中元素
    """
    s = {'a', 'cc', 'f'}
    s.pop()
    print(s)   # 输出不确定是哪两个元素
    
    """
    set.clear() 清空集合
    """
    s = {'a', 'cc', 'f'}
    s.clear()
    print(s)    # set()
    
    
    """
    len(s) 计算集合元素的个数
    """
    s = {'a', 'cc', 'f'}
    print(len(s))
    
    """
    差集(-),或者使用set.difference(s)
    """
    s = {'a', 'cc', 'f'}
    s1 = {'a', 'f', 1, 'ww'}
    # 两种求差集的方法
    print("在s中不在s1中: " + str(s.difference(s1)))   # {'cc'}
    print('在s1中不在s中: '+ str(s1 - s))             # {1, 'ww'}
    
    """
    交集(&),或者使用set.intersection(s)
    """
    s = {'a', 'cc', 'f'}
    s1 = {'a', 'f', 1, 'ww'}
    # 同时在集合s 和 s1 中的元素
    print(s.intersection(s1))   # {'f', 'a'}
    print(s1 & s)               # {'f', 'a'}
    
    """
    并集(|),或者使用set.union(s)
    """
    s = {'a', 'cc', 'f'}
    s1 = {'a', 'f', 1, 'ww'}
    # 元素在集合 s 中或在集合 s1 中
    print(s.union(s1))    # {'ww', 1, 'cc', 'a', 'f'}
    print(s1 | s)         # {'ww', 1, 'cc', 'a', 'f'}
    
    """
    对称差集(^),或者使用set.sysmmetric_difference(s)
    """
    s = {'a', 'cc', 'f'}
    s1 = {'a', 'f', 1, 'ww'}
    # 除集合s和集合s1共有的以外的元素
    print(s.symmetric_difference(s1))   # {'ww', 1, 'cc'}
    print(s1 ^ s)                       # {'ww', 'cc', 1}
    
    """
    set.issubset(x) 判断一个集合是否是另一个集合的子集
    """
    s = {'a', 'cc', 'f'}
    s1 = {'a', 'f'}
    print(s.issubset(s1))    # False
    print(s1.issubset(s))    # True
    
    """
    set.isuperset(x) 判断一个集合是否是另一个集合的父集
    """
    s = {'a', 'cc', 'f'}
    s1 = {'a', 'f'}
    print(s.issuperset(s1))    # True
    print(s1.issuperset(s))    # False
    # s1是s的子集,s是s1的父集
    print(s1.issubset(s))      # True
    
    """
    set.isdisjoint(x) 检测2个集合是否不存在交集 存在交集 False
    """
    s1 = {'ljl','wc','xy','zb','lsy'}
    s2 = {'mmf','lsy','syj'}
    s3 = {1, 2}
    print(s1.isdisjoint(s2))   # False
    print(s1.isdisjoint(s3))   # True
    
    """
    set.intersection_update(s) 用两者的交集去更新集合
    """
    s1 = {'ljl','wc','xy','zb','lsy'}
    s2 = {'mmf','lsy','syj'}
    s1.intersection_update(s2)
    s2.intersection_update(s1)
    print(s1)   # {'lsy'}
    print(s2)   # {'lsy'}
    

     因为set的元素唯一性,所以经常用来去重,比如:在一个序列上面保持元素顺序的同时消除重复的值。

    """
    对于列表类型去重
    """
    def dedupe1(items):
        seen = set()
        for item in items:
            if item not in seen:
                yield item
            seen.add(item)
            
    a = [1, 5, 2, 1, 9, 1, 5, 10]
    print(list(dedupe1(a)))     # [1, 5, 2, 9, 10]
    
    """
    对于字典类型去重
    """
    def dedupe2(items, key=None):
        seen = set()
        for item in items:
            val = item if key is None else key(item)
            if val not in seen:
                yield item
            seen.add(val)
    
    a = [ {'x': 1, 'y': 2}, {'x': 1, 'y':3}, {'x': 1, 'y': 2}, {'x': 2, 'y': 4}]
    print(list(dedupe2(a, key=lambda d: (d['x'],d['y']))))   # [{'x': 1, 'y': 2}, {'x': 1, 'y': 3}, {'x': 2, 'y': 4}]
    

    集合推导式:语法与列表生成式一致,只是把()改成{}。

    lst = [1, -1, 8, -8, 12] 
    # 绝对值去重
    s = { abs(i) for i in lst }
    print(s)
    

      

  • 相关阅读:
    字符编码相关
    函数之形参与实参
    文件操作模式
    函数对象,名称空间,作用域,和闭包
    吴裕雄天生自然SPRINGBOOT开发实战处理'spring.datasource.url' is not specified and no embedded datasource could be autoconfigured
    吴裕雄天生自然SPRINGBOOT开发实战处理XXXX that could not be found.
    吴裕雄天生自然SPRINGBOOT开发实战SpringBoot HTML表单登录
    吴裕雄天生自然SPRINGBOOT开发实战SpringBoot REST示例
    吴裕雄天生自然SpringBoot开发实战学习笔记处理 Could not write metadata for '/Servers'.metadata\.plugins\org.eclipse.core.resources\.projects\Servers\.markers.snap (系统找不到指定的路径。)
    吴裕雄天生自然SPRINGBOOT开发实战SpringBoot Tomcat部署
  • 原文地址:https://www.cnblogs.com/yanghh/p/13193756.html
Copyright © 2011-2022 走看看