zoukankan      html  css  js  c++  java
  • python学习第三天-集合

    增:

      add

    a = {1,2,3}
    a.add(4)
    print(a)
    #{1, 2, 3, 4}

    删:

      discard、remove、pop

    a = {1,2,3}
    a.remove(1)
    print(a)
    a.discard(1)
    print(a)
    a.discard(2)
    print(a)
    #{2, 3}
    #{2, 3}
    #{3}

    改(不支持,也不支持切片,无索引):

    a = {1,2,3}
    print(a[0])
    # File "E:/pythondir/Day01/task02.py", line 2, in <module>
        print(a[0])
    TypeError: 'set' object is not subscriptable

    查:

      in or not in 

    并集:

      | 、union

    a = {1,2,3,4,5}
    b = {2,3,5,6,7,8,9}
    print(a | b)
    print(a.union(b))
    #{1, 2, 3, 4, 5, 6, 7, 8, 9}
    #{1, 2, 3, 4, 5, 6, 7, 8, 9}

    差集:

     -、difference 

    a = {1,2,3,4,5}
    b = {2,3,5,6,7,8,9}
    print(a - b)
    print(a.difference(b))
    print(a.difference_update(b))  #去掉交集,并更新
    print(a)
    #{1, 4}
    #{1, 4}
    #None
    #{1, 4}

    交集:

      &、intersection

    a = {1,2,3,4,5}
    b = {2,3,5,6,7,8,9}
    print(a & b)
    print(a.intersection(b))
    #{2, 3, 5}
    #{2, 3, 5}

    对称差集(两者并集去掉交集):

      ^、symmetric_difference

    a = {1,2,3,4,5}
    b = {2,3,5,6,7,8,9}
    print(a ^ b)
    print(a.symmetric_difference(b))
    #{1, 4, 6, 7, 8, 9}
    #{1, 4, 6, 7, 8, 9}
  • 相关阅读:
    cordova的安装与配置
    JavaScript-string
    JavaScript-Number
    android-适配器
    网络操作-转码(乱码情况处理)
    网络操作-请求优先级
    Android读取权限
    I/O-<File区别>
    I/O-<File实例>
    I/O-<文件读写、输出>
  • 原文地址:https://www.cnblogs.com/thanos-ryan/p/13258136.html
Copyright © 2011-2022 走看看