zoukankan      html  css  js  c++  java
  • 09-Python-集合

    1、集合

    集合是一个无序的,不重复的数据组合,它的主要作用如下:

    • 去重,把一个列表变成集合,就自动去重了。
    • 关系测试,测试两组数据之前的交集、差集、并集等关系。

    2、定义集合

    setA = {1, 2, 3, 4, 4, }
    print(setA)
    {1, 2, 3, 4}
    print(type(setA))
    <class 'set'>

    3、集合的关系操作

     1 list_1 = [1,3,4,5,7,8,9,3,4]
     2 
     3 list_1 = set(list_1)  #转换为集合。自动去重。
     4 print(list_1,type(list_1))
     5 
     6 list_2 = set([0,1,2,6,8,10])  #创建集合。
     7 print(list_2)
     8 print("33[31;1m-----------33[0m")
     9 #交集
    10 print(list_1.intersection(list_2))
    11 print(list_1 & list_2)
    12 print("33[31;1m-----------33[0m")
    13 #并集
    14 print(list_1.union(list_2))
    15 print(list_1 | list_2)
    16 print("33[31;1m-----------33[0m")
    17 #差集
    18 print(list_1.difference(list_2))  #在list_1中有,但是list_2中没有的。
    19 print(list_1 - list_2)  #在list_1中有,但是list_2中没有的。
    20 print(list_2.difference(list_1))  #在list_2中有,但是list_1中没有的。
    21 print(list_2 - list_1)  #在list_2中有,但是list_1中没有的。
    22 #对称差集
    23 print("33[31;1m-----------33[0m")
    24 print(list_1.symmetric_difference(list_2))  #取出两个集合中都没有元素
    25 print(list_2.symmetric_difference(list_1))
    26 print(list_1 ^ list_2) 
    27 print("33[31;1m-----------33[0m")
    28 #子集
    29 print(list_1.issubset(list_2))  #判断list_1是否为list_2的子集
    30 list_3 = set([1,3,4])
    31 print(list_3.issubset(list_1))  #判断list_3是否为list_1的子集
    32 print("33[31;1m-----------33[0m")
    33 #父集
    34 list_4 = set([0,1,2,6,8,10,11,12])
    35 print(list_4.issuperset(list_2))  #判断list_4是否为list_2的父集
    36 print("33[31;1m-----------33[0m")
    37 #
    38 list_5 = set([0,2,4])
    39 list_6 = set([1,3,5])
    40 
    41 print(list_5.isdisjoint(list_6))  #是否有交集

    4、集合的常用操作

     1 list_1 = [1,3,4,5,7,8,9,3,4]
     2 list_1 = set(list_1)  #转换为集合。自动去重。
     3 
     4 list_2 = set([0,1,2,6,8,10])  #创建集合。
     5 print("33[31;1m-----------33[0m")
     6 
     7 list_1.add(666)  #添加一个元素
     8 print(list_1)
     9 print("33[31;1m-----------33[0m")
    10 
    11 list_1.update([11,22,33])  #添加多个元素
    12 print(list_1)
    13 print("33[31;1m-----------33[0m")
    14 
    15 list_1.remove(11)  #移除一个指定的元素,如果没有,则报错。
    16 print(list_1)
    17 print("33[31;1m-----------33[0m")
    18 
    19 list_1.discard(22)  #删除一个元素,如果这个元素不在集合中,则不进行任何操作。
    20 print(list_1)
    21 print("33[31;1m-----------33[0m")
    22 
    23 print(list_1.pop())  #随机删除一个元素,并且返回删除的元素
    24 print("33[31;1m-----------33[0m")
    25 
    26 print(len(list_1))  #求长度
    27 print("33[31;1m-----------33[0m")
    28 
    29 print(33 in list_1)  #元素是否在集合中
    30 print("33[31;1m-----------33[0m")
  • 相关阅读:
    准备 FRM 考试——方法、工具与教训
    930. 和相同的二元子数组 前缀和
    1906. 查询差绝对值的最小值 前缀和
    剑指 Offer 37. 序列化二叉树 二叉树 字符串
    815. 公交路线 BFS
    518. 零钱兑换 II dp 完全背包
    1049. 最后一块石头的重量 II dp
    5779. 装包裹的最小浪费空间 二分
    5778. 使二进制字符串字符交替的最少反转次数 字符串 滑动窗口
    474. 一和零 dp
  • 原文地址:https://www.cnblogs.com/Druidchen/p/7742096.html
Copyright © 2011-2022 走看看