• Python自动化开发数据类型之集合


    Python 数据类型-集合

    集合也是Python数据类型之一。接下来主要介绍集合类型的操作。

    1.创建集合

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    #创建集合
    jh0101={11,22,"lucy",(1,"sdf")}
    print(jh0101)
    print(type(jh0101))
    View Code

    代码运行结果:

    {11, 'lucy', (1, 'sdf'), 22}
    <class 'set'>

    2.求交集

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    #求交集
    jh01={22,333,"asdf",("dsdf","12we",11)}
    jh02={22,"asdf",("dsdf","12we",11)}
    jiaoji=jh02.intersection(jh01)
    print(jiaoji)
    View Code

    代码运行结果:

    {'asdf', ('dsdf', '12we', 11), 22}

    3.列表转换为集合

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    #列表转换为集合
    jh01 = set(["qwe", 123])
    print(jh01)
    print(type(jh01))
    View Code

    代码运行结果:

    {123, 'qwe'}
    <class 'set'>

    4.求并集

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    #求并集
    jh01={11,22,333}
    jh02={333,13,22}
    bj=jh01.union(jh02)
    print(bj)
    View Code

    代码运行结果:

    {13, 22, 11, 333}

     5.求差集

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    #求差集
    jh01={11,22,333}
    jh02={333,13,22}
    cj01=jh02.difference(jh01)
    print(cj01)
    cj02=jh01.difference(jh02)
    print(cj02)
    View Code

    代码运行结果:

    {13}
    {11}

    6.求交叉补集

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    #求交叉补集
    jh01={11,22,333}
    jh02={333,13,22}
    jcbj01=jh01.symmetric_difference(jh02)
    print(jcbj01)
    jcbj02=jh02.symmetric_difference(jh01)
    print(jcbj02)
    View Code

    代码运行结果:

    {11, 13}
    {11, 13}

    7.向集合中添加元素/浅复制/随机删除集合中的某个元素/清空集合

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    jh01={11,22,"dfre",(22,"dfgh","g",)}
    print(jh01)
    jh01.add("mb1222")
    print(jh01)
    qiancopy=jh01.copy()
    print(qiancopy)
    suijishan=jh01.pop()
    print(jh01)
    jh01.clear()
    print(jh01)
    View Code

    代码运行结果:

    {'dfre', 11, (22, 'dfgh', 'g'), 22}
    {'dfre', 11, 'mb1222', (22, 'dfgh', 'g'), 22}
    {'dfre', 11, 'mb1222', (22, 'dfgh', 'g'), 22}
    {11, 'mb1222', (22, 'dfgh', 'g'), 22}
    set()

    8.删除集合指定元素,被删除的元素不存在会报错

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    #指定删除集合中某个元素,被删除的元素不存在会报错
    jh01={11,22,333,"hello",(11,22,333,)}
    jh01.remove("hellodddd")
    print(jh01)
    View Code

    代码运行结果:

    C:\Users\Administrator\PycharmProjects\python_s3\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/python_s3/day10/s02.py
    Traceback (most recent call last):
    File "C:/Users/Administrator/PycharmProjects/python_s3/day10/s02.py", line 6, in <module>
    jh01.remove("hellodddd")
    KeyError: 'hellodddd'

    9.删除集合指定元素,被删除的元素不存在不会报错

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    #指定删除集合中某个元素,被删除的元素不存在不会报错
    jh01={11,22,333,"hello",(11,22,333,)}
    jh01.discard(222)
    print(jh01)
    View Code

    代码运行结果:

    {11, 'hello', 333, 22, (11, 22, 333)} 

    你不向我走来,我便向你走去。
  • 相关阅读:
    递归函数思想理解
    关于C++11 模板中的 using
    DES与3DES
    c++ 内存
    单词替换程序demo
    GF(256)下数的乘法 转化为矩阵乘法
    码片速率的含义
    转载 WCDMA中码片速率、符号速率、bit速率 WCDMA常用概念
    LTE 到GSM 的CCO过程是怎样的【转载自360】
    转载自搜狐科技【技术那些事儿】LTE网络中的用户数据库HSS与传统2G/3G的HLR有何区别?能否融合组网?
  • 原文地址:https://www.cnblogs.com/renyongbin/p/15722036.html
Copyright © 2020-2023  润新知