zoukankan      html  css  js  c++  java
  • 作业16:集合及深浅

    # 集合:可变的数据类型,里面存的元素必须是不可变的数据类型,集合无序,但是不重复(功能去重复)
    # 列表转化为集合就可以去重复
    # {}
    
    # set = ({1,2,3})
    # print(set)  # 结果{1, 2, 3} 这就是集合  如果放入可变元素 会报错
    
    
    # set1 = {'elsa','alex','tom','mike','mike'}  # 如果有重复的元素 可自动去重复
    # 增
    # add 增加
    # set1.add('女神')
    # print(set1)  # 结果 {'mike', '女神', 'alex', 'tom', 'elsa'} 加入无序是随机的
    # # update
    # set1.update('abc')
    # print(set1)  # 结果 {'a', '女神', 'elsa', 'b', 'c', 'alex', 'mike', 'tom'}
    
    # 删
    # set1.pop()   # 随机删除集合中的元素  返回值
    # print(set1.pop())
    # print(set1)
    
    # set1.remove('mike')  # 按元素删除
    # print(set1)  #{'elsa', 'alex', 'tom'}
    
    # set1.clear()  #清空集合
    # print(set1)  # 结果 set()
    #
    # del(set1) # 删除集合
    # print(set1)
    
    # 查
    # 只能用for循环来查询
    
    # for i in set1:
    #     print(i)
    # 结果 且 顺序是随机的  没有索引、键值对
    # alex
    # tom
    # elsa
    # mike
    
    
    # set1 = {1,2,3,4,5,6}
    # set2 = {4,5,6,7,8,9}
    # 求集合正交集  “&” 交集的连接符  intersection
    # set3 = set1 & set2
    # print(set1 & set2)  # 结果 {4, 5, 6}
    # print(set1.intersection(set2))  # 结果 {4, 5, 6}
    # print(set3) # 结果 {4, 5, 6}
    
    # 求集合反交集  “^” 反交集连接符
    # print(set1 ^ set2)  # 结果 {1, 2, 3, 7, 8, 9}
    
    # 差集
    # print(set1 - set2) # 结果 {1, 2, 3}  set1独有的元素
    # print(set2 - set1) # 结果 {8, 9, 7}  set2独有的元素
    
    # 求集合并集  “|” 管道符 union 连接
    # print(set1 | set2)  # 结果 {1, 2, 3, 4, 5, 6, 7, 8, 9}
    # print(set2.union(set1))  # 结果 {1, 2, 3, 4, 5, 6, 7, 8, 9}
    
    # 子集与超集
    
    # set1 = {1,2,3}
    # set2 = {1,2,3,4,5,6}
    # print(set1 < set2) # 结果 True  说明 set1是ste2的子集
    # print(set2 > set1) # 结果 True  说明 set2是ste1的超集
    
    # 列表去重
    # li = [1,23,34,34,33,22,22,33]
    # m = set(li)  # 转化为集合去重   结果 {1, 34, 33, 22, 23}
    # li = list(m) # 在将集合转化为列表  结果 [1, 34, 33, 22, 23]
    # print(li)
    
    # 冻结集合
    # s = frozenset('abcd')
    # print(s)  # 结果 frozenset({'c', 'a', 'b', 'd'})
    # 无序 且 为只读  不能  增、删
    

      

  • 相关阅读:
    每天一篇文献:A SURVEY OF LEARNING FROM DEMONSTRATION USED IN ROBOTICS
    PyBullet(七)在PyBullet中使用VR
    期刊模板搜索网址
    论文阅读:Robot Program Parameter Inference via Differentiable Shadow Program Inversion
    visio画图如何插入到latex中
    win10下TensorFlow-GPU安装(GTX1660+CUDA10+CUDNN7.4)
    Object detection with localization using Unity Barracuda and ARFoundation
    论文阅读:Design and Implementation of a Virtual Reality Application for Mechanical Assembly Training
    Qt开发经验小技巧151-155
    Qt开发经验小技巧146-150
  • 原文地址:https://www.cnblogs.com/elsa2007/p/10952975.html
Copyright © 2011-2022 走看看