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

    set集合

       

     1、 创建集合并去重

          方法一 :     {}

    one_set = set([10,30,10,20,40,11,20,30,12])
    print(one_set)
    print(type(one_set))
    

      

      结果:

         

         方法二:set([ ])

    one_set = set([10,30,10,20,40,11,20,30,12])
    print(one_set)
    print(type(one_set))

      结果:

          

      2、 创建空集合

          不能使用 {} 来创建空集合,默认创建的是空字典

          使用set:

    empty_set = set()
    print(empty_set)
    print(type(empty_set))
    

      结果:

        

       3、 求交集   

           方法一:  &

    one_set ={10,30,10,20,40,11,20,30,12}
    two_set ={10,30,60,90,80,76}
    result_set1 = one_set & two_set
    print(result_set1)
    

      结果:

         

     

          方法二:   one_set.intersection(two_set)

    one_set ={10,30,10,20,40,11,20,30,12}
    two_set ={10,30,60,90,80,76}
    result_set11 = one_set.intersection(two_set)
    print(result_set11)
    

      结果:

          

      4、 求并集

            方法一       |

    one_set ={10,30,10,20,40,11,20,30,12}
    two_set ={10,30,60,90,80,76}
    result_set2 = one_set | two_set
    print(result_set2)
    

      结果:

        

         

           方法二:    one_set.union(two_set)

    one_set ={10,30,10,20,40,11,20,30,12}
    two_set ={10,30,60,90,80,76}
    result_set22 = one_set.union(two_set)
    print(result_set22)
    

      结果:

         

      5、  求差集

          方法一 :      -

    one_set ={10,30,10,20,40,11,20,30,12}
    two_set ={10,30,60,90,80,76}
    result3 = one_set - two_set
    print(result3)
    

      结果:

          

          方法二:

    one_set ={10,30,10,20,40,11,20,30,12}
    two_set ={10,30,60,90,80,76}
    result33 = one_set.difference(two_set)
    print(result33)
    

      结果:

          

  • 相关阅读:
    python 如何把在字符串里面的名字变成变量,进行复制
    2017-05-30 英语
    RYU 中如钩构建TCP数据包,设置ACK等标志
    Arch linux LXR 安装过程
    ubuntu如何配置lxr
    2017-05-27 英语
    Emacs学习笔记:移动
    9.特殊矩阵的压缩存储
    13.链路层设备
    9.CSMA_CD协议
  • 原文地址:https://www.cnblogs.com/taiyangfeng/p/12578094.html
Copyright © 2011-2022 走看看