zoukankan      html  css  js  c++  java
  • Python3之set集合

      1 # Author:Allister.Liu
      2 
      3 """
      4  set 集合是一个无序且不重复的数据组合
      5     作用:
      6         1、去重;
      7         2、关系测试,测试两组数据的交集,差集,并集等关系
      8 """
      9 
     10 
     11 list_1 = [1, 3, 4, 8, 6, 4, 6, 9]
     12 list_1 = set(list_1)
     13 
     14 list_2 = set([2, 5, 7, 8, 22, 45, 3])
     15 list_3 = set([1, 4, 6, 9])
     16 list_4 = set([2, 5, 7])
     17 
     18 
     19 # 结果:{1, 3, 4, 6, 8, 9} <class 'set'> 已去重
     20 # print(list_1, type(list_1))
     21 
     22 
     23 print("list_1:", list_1, "list_2:", list_2, "list_3:", list_3, "list_4:", list_4)
     24 print("性感的分割线".center(60, "~"))
     25 # 交集
     26 print("交集", list_1.intersection(list_2))
     27 # 并集 两个合成一块
     28 print("并集", list_1.union(list_2))
     29 # 差集 list_1有的list_2没有的
     30 print("差集", list_1.difference(list_2))
     31 
     32 
     33 # 子集 list_1是否包含list_3
     34 print("子集", list_3.issubset(list_1))
     35 # 父集
     36 print("父集", list_1.issuperset(list_3))
     37 
     38 # 对称差集 两个互相没有的  也就是去掉交集
     39 print("对称差集", list_1.symmetric_difference(list_2))
     40 
     41 # Return True if two sets have a null intersection. 只要有一个交集就返回False,反之为True
     42 print(list_3.isdisjoint(list_4))
     43 
     44 
     45 """
     46 使用运算符号
     47 """
     48 print("性感的分割线,下面使用运算符号".center(60, "~"))
     49 # 并集
     50 print("并集:", list_1 | list_2)
     51 # 交集
     52 print("交集", list_1 & list_2)
     53 # 差集 list_1有的list_2没有的
     54 print("", list_1 - list_2)
     55 # 对称差集
     56 print("", list_1 ^ list_2)
     57 # 子集 list_1是否包含list_3
     58 print(list_3 <= list_1)
     59 # 父集
     60 print(list_1 >= list_3)
     61 
     62 
     63 """
     64 集合的操作
     65     添加:
     66        add(x):添加一个x
     67        update([x1, x2, x3, ...]): 添加多个x1, x2, x3...
     68     
     69     删除:
     70         s.remove(x) : 删除指定元素x,如果x不存在s集合中,则会报错
     71         s.pop() : 删除任意一个并返回被删除的成员
     72         s.discard(x) : 删除指定成员x,如果x存在s集合中,则删除,不存在,什么都不做,也不会报错
     73         
     74     验证是否存在:
     75         x in s : x是否是s集合的成员
     76         x not in s : x是否不是s集合的成员
     77 """
     78 print("集合的操作".center(50, "^"))
     79 # 新增
     80 list_1.add(999)
     81 list_1.update([888, 777, 666])
     82 
     83 # 删除
     84 list_1.remove(999)
     85 
     86 # 删除任意一个并返回被删除的成员
     87 # print(list_1.pop())
     88 # print(list_1.pop())
     89 # print(list_1.pop())
     90 
     91 # 删除指定成员,如果存在则删除,不存在什么都不干,不会报错
     92 list_1.discard(666)
     93 print(list_1)
     94 
     95 # 判断某个元素是否存在集合中
     96 print(3 in list_1)
     97 # 判断某个元素是否不存在集合中
     98 print(333 not in list_1)
     99 
    100 # 返回list_1的一份浅复制
    101 list_1.copy()
    list_1: {1, 3, 4, 6, 8, 9} list_2: {2, 3, 5, 7, 8, 45, 22} list_3: {1, 4, 6, 9} list_4: {2, 5, 7}
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~性感的分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~
    交集 {8, 3}
    并集 {1, 2, 3, 4, 5, 6, 7, 8, 9, 45, 22}
    差集 {1, 4, 6, 9}
    子集 True
    父集 True
    对称差集 {1, 2, 4, 5, 6, 7, 9, 22, 45}
    True
    ~~~~~~~~~~~~~~~~~~~~~~性感的分割线,下面使用运算符号~~~~~~~~~~~~~~~~~~~~~~~
    并集: {1, 2, 3, 4, 5, 6, 7, 8, 9, 45, 22}
    交集 {8, 3}
     {1, 4, 6, 9}
     {1, 2, 4, 5, 6, 7, 9, 22, 45}
    True
    True
    ^^^^^^^^^^^^^^^^^^^^^^集合的操作^^^^^^^^^^^^^^^^^^^^^^^
    {1, 3, 4, 6, 8, 9, 777, 888}
    True
    True
    执行结果

    键盘上的武者

  • 相关阅读:
    692. 前K个高频单词
    准备工作:更新代码和运行环境
    1319. 连通网络的操作次数——并查集
    <leetcode c++>25. K 个一组翻转链表
    织梦dedecms手机站关闭自动生成首页index.html
    IIS7 IIS7.5 伪静态 web.config 配置方法不带WWW的301跳转到带WWW
    win7和xp一样有左下角显示桌面快捷方式
    Win7系统传真与扫描功能无法使用的处理方法
    织梦dedecms将列表页重复的第一页去除的方法
    秦岭土蜂蜜价格 秦岭土蜂蜜多少钱一斤
  • 原文地址:https://www.cnblogs.com/allister/p/8279658.html
Copyright © 2011-2022 走看看