• Python集合及其运算


    集合(set)

    集合是由不同可hash的值组成的,里面所有的值都是唯一的,也是无序的

    集合的创建

    >>>set_test = {"name", "age", "hometown"}
    ##  把值放入{ }中
    
    >>>lis = ("name", "age", "hometown")
    >>>set_test = set(lis)
    >>> set_test
    {'name', 'age', 'hometown'}
    ##利用set()函数,把其他类型转换为set
    
    >>> list_test = ["name", "age", "hometown"]
    >>> set_test = frozenset(list_test)
    >>> set_test
    frozenset({'name', 'age', 'hometown'})
    ##利用frozenset()函数可以创造一个set,并把它放到元组里面
    

    集合的操作

    • add(self, *args, **kwargs)

      添加值到集合里面

      >>> set_test
      {'name', 'age', 'hometown'}
      >>> set_test.add("hobby")
      >>> set_test
      {'name', 'age', 'hobby', 'hometown'}
      
    • clear(self, *args, **kwargs)

      清空集合

    • copy(self, *args, **kwargs)

      浅拷贝

    • discard(self, *args, **kwargs)

      删除某值,没有时不会报错

      >>> set_test
      {'name', 'age', 'hobby', 'hometown'}
      >>> set_test.discard("hobby")
      >>> set_test
      {'name', 'age', 'hometown'}
      
    • pop(self, *args, **kwargs)

      当集合是由列表和元组组成时,set.pop()是从左边删除元素的,并且可以得到被删除的值

      >>> set_test
      {'name', 'age', 'hometown'}
      >>> set_test.pop()
      'name'
      >>> set_test.pop()
      'age'
      
    • remove(self, *args, **kwargs)

      删除某值,但是如果没有的话,就会报错

      >>> set_test = {'name', 'age', 'hometown'}
      >>> set_test.remove("hometown")
      >>> set_test
      {'name', 'age'}
      
    • update(self, *args, **kwargs)

      更新值,可以在set里面添加多个值

      >>> set_test = {"Jack", "Mark", "Ada"}
      >>> set_test.update(["Jewish", "Obanma", "Anna"])
      >>> print(set_test)
      {'Obanma', 'Mark', 'Jack', 'Anna', 'Ada', 'Jewish'}
      

    集合的运算

    • 求交集

      >>> set_test1 = {"YuanMing", "Hermaeus", "Chenglong", "Kelan"}
      >>> set_test2 = {"YuanMing", "Hermaeus", "ZhangJie","Jack"}
      
      >>> test_result = set_test1.intersection(set_test2)
      >>> print(test_result)
      {'YuanMing', 'Hermaeus'}
      
      >>> test_result = set_test1&set_test2
      >>> print(test_result)
      {'YuanMing', 'Hermaeus'}
      
    • 求并集

      >>> set_test1 = {"YuanMing", "Hermaeus", "Chenglong", "Kelan"}
      >>> set_test2 = {"YuanMing", "Hermaeus", "ZhangJie","Jack"}
      
      >>> test_result = set_test1.union(set_test2)
      >>> print(test_result)
      {'Kelan', 'YuanMing', 'ZhangJie', 'Jack', 'Hermaeus', 'Chenglong'}
      
      >>> test_result = set_test1|set_test2
      >>> print(test_result)
      {'Kelan', 'YuanMing', 'ZhangJie', 'Jack', 'Hermaeus', 'Chenglong'}
      
    • 求差集

      >>> set_test1 = {"YuanMing", "Hermaeus", "Chenglong", "Kelan"}
      >>> set_test2 = {"YuanMing", "Hermaeus", "ZhangJie","Jack"}
      
      >>> test_result = set_test1.difference(set_test2)
      >>> test_result1 = set_test1.difference(set_test2)
      >>> print(test_result1)
      {'Kelan', 'Chenglong'}
      
      >>> test_result1 = set_test1-set_test2
      >>> print(test_result1)
      {'Kelan', 'Chenglong'}
      
      >>> test_result2 = set_test2.difference(set_test1)
      >>> print(test_result2)
      {'ZhangJie', 'Jack'}
      
      >>> test_result2 = set_test2-set_test1
      >>> print(test_result2)
      {'ZhangJie', 'Jack'}
      
    • 求交叉补集

      >>> set_test1 = {"YuanMing", "Hermaeus", "Chenglong", "Kelan"}
      >>> set_test2 = {"YuanMing", "Hermaeus", "ZhangJie","Jack"}
      >>> test_result = set_test1.symmetric_difference(set_test2)
      >>> print(test_result)
      {'ZhangJie', 'Jack', 'Kelan', 'Chenglong'}
      >>> test_result = set_test1^set_test2
      >>> print(test_result)
      {'ZhangJie', 'Jack', 'Kelan', 'Chenglong'}
      
    • 求集赋值

      • symmetric_difference_update(self, *args, **kwargs)

        >>> set_test1 = {"Jack", "Mark", "Devid"}
        >>> set_test2 = {"Jack", "Marish", "Good"}
        
        >>> set_test1.symmetric_update(set_test2)
        >>> set_test1.symmetric_difference_update(set_test2)  ## 把得到值赋予set_test1
        >>> set_test1, set_test2
        ({'Good', 'Marish', 'Devid', 'Mark'}, {'Good', 'Jack', 'Marish'})
        
        >>> set_test1 ^= set_test2			 
        >>> set_test1			 
        {'Good', 'Marish', 'Devid', 'Mark'}
        
      • intersection_update(self, *args, **kwargs)

        >>> set_test1 = {"Jack", "Mark", "Devid"}
        >>> set_test2 = {"Jack", "Marish", "Good"}
        
        >>> set_test1.intersection_update(set_test2)  ##把得到的值赋予set_test1
        >>> set_test1, set_test2
        ({'Jack'}, {'Good', 'Jack', 'Marish'})
        
        >>> set_test1 &= set_test2			 
        >>> set_test1			 
        {'Jack'}
        
      • difference_update(self, *args, **kwargs)

        >>> set_test1 = {"Jack", "Mark", "Devid"}
        >>> set_test2 = {"Jack", "Marish", "Good"}
        
        >>> set_test1.difference_update(set_test2)
        >>> set_test1, set_test2
        ({'Devid', 'Mark'}, {'Good', 'Jack', 'Marish'})
        >>> set_test1 = {"Jack", "Mark", "Devid"}			 
        >>> set_test2 = {"Jack", "Marish", "Good"}	
        
        >>> set_test1 -= set_test2			 
        >>> set_test1			 
        {'Devid', 'Mark'}
        
      • set_test1 |= set_test2

        >>> set_test1 = {"Jack", "Mark", "Devid"}			 
        >>> set_test2 = {"Jack", "Marish", "Good"}
        >>> set_test1 |= set_test2			 
        >>> set_test1		
        

    子集与父集

    • issubset(self, *args, **kwargs)

      A.issubset(B) A是B的子集吗? 返回bool

      >>> set_test2 = {"Jack", "Marish", "Good"}
      >>> set_test1 = {"Jack"}
      >>> set_test2.issubset(set_test1)
      False
      >>> set_test1.issubset(set_test2)
      True
      
    • issuperset(self, *args, **kwargs)

      A.issuperset(B) A是B的父集吗? 返回bool

      >>> set_test2 = {"Jack", "Marish", "Good"}
      >>> set_test1 = {"Jack"}
      >>> set_test1.issuperset(set_test2)
      False
      >>> set_test2.issuperset(set_test1)			 
      True
      
    • isdisjoint(self, *args, **kwargs)

      返回True如果两个set没有交集

      >>> set_test1 = {"Jack", "Mark", "Devid"}		 
      >>> set_test2 = {"Ada", "Marish", "Good"}			 
      >>> set_test1.isdisjoint(set_test2)			 
      True
      
    • <,<= OR >,>=

      >>> set_test1 = {"Jack", "Mark", "Devid"}			 
      >>> set_test2 = {"Jack", "Mark"
      >>> set_test1 < set_test2			 
      False
      >>> set_test1 > set_test2			 
      True
      
  • 相关阅读:
    Spring框架介绍及使用
    SpringMVC 网站
    Maven网站
    mysql数据库忘记密码时如何修改
    搭建ssm的步骤
    maven私服 Nexus2.x.x私服安装配置
    搭建聚合工程教案
    SVN上传下载项目
    如何区分不同用户——Cookie/Session机制详解
    java中Token验证
  • 原文地址:https://www.cnblogs.com/MingleYuan/p/10628520.html
Copyright © 2020-2023  润新知