zoukankan      html  css  js  c++  java
  • python-集合

    一、集合:set

    特点:

    天生去重;集合也是无序的

    不可根据下标取值,可用for循环取出集合中的元素,如以下代码

    1 nums  = {6,7,1,2,3,4,5,6,7}# 集合可自动去重
    2 for n in nums: # 使用for循环读取集合中的数据
    3     print(n)

     1、集合的定义

    (1)定义一个空集合,如下所示

    *注:创建空集合时,只能用set(),如果用第二种方法s={},创建的实际上是一个空字典。
    1 num_set = set()
    2 print(num_set) #运行结果:set()
    3 print(type(num_set))  #第一种方法,也是正确的创建集合方法,运行结果:<class 'set'>
    4 s = {}
    5 print(s)# 运行结果:{}
    6 print(type(s)) # 第二种方法,错误的方法,实际创建的是一个字典,运行结果:<class 'dict'>

    (2)将list定义为集合,set[list],如下所示

     1 nums = [1,2,3,4,2,4,5] # 定义一个list

    2 print(set(nums))# 运行结果:{1, 2, 3, 4, 5} 

     二、集合的关系测试-交集&、并集|、差集-、(反向差集、对称差集^)

    1 # 首先定义两个集合

    2 num = {1,2,5,6,8,9,0}

    3 num2 = {1,4,7,8,9,12,15} 

    1、交集&

    取出两个集合中重复的数据,有两种方式,$比较好记,另一种了解即可

    1 print(num.intersection(num2) )# 运行结果:{8, 1, 9}

    2 print(num & num2)# 运行结果:{8, 1, 9} 

    附:对称差集^

    将两个集合中重复的数据都去掉后,取剩余的数据(或称对称差集)

    1 print(num.symmetric_difference(num2)) # 运行结果:{0, 2, 4, 5, 6, 7, 12, 15}
    2 print(num ^ num2)# 运行结果:{0, 2, 4, 5, 6, 7, 12, 15}

    2、并集|

    将两个集合合并,同样是两种方式,|容易记得,另一种仅需了解

    1 print(num.union(num2))# 运行结果:{0, 1, 2, 4, 5, 6, 7, 8, 9, 12, 15}
    2 print(num | num2)# 运行结果:{0, 1, 2, 4, 5, 6, 7, 8, 9, 12, 15}

    3、差集-

    把num集合中存在,num2中不存在的数据取出来,-比较好记,另一种了解即可

    1 print( num - num2 )# 运行结果:{0, 2, 5, 6}

    2 print(num.difference(num2))# 运行结果:{0, 2, 5, 6} 

     三、集合操作

     1 num.add(777) #一次添加一个
     2 print(num)# 运行结果:{0, 1, 2, 5, 6, 8, 9, 777}
     3 num.update([888,999])#同时添加多个
     4 print(num)# 运行结果:{0, 1, 2, 5, 6, 999, 8, 9, 777, 888}
     5 num.remove(999)# 删除指定数据
     6 print(num)# 运行结果:{0, 1, 2, 5, 6, 8, 9, 777, 888}
     7 num.pop()# 随机删除一个数据(先将集合排序后,删除最小的一个)
     8 print(num)# 运行结果:{1, 2, 5, 6, 8, 9, 777, 888}
     9 num.discard(888) # 删除一个不存在的 不会报错
    10 print(num)# 运行结果:{1, 2, 5, 6, 8, 9, 777}

    四、练习

    1、

     1 import string
     2 all_nums = set(string.digits)
     3 print(all_nums)# {'7', '1', '6', '3', '5', '4', '9', '0', '8', '2'}
     4 num4 = {'1','2','3'}
     5 print(all_nums.issuperset(num4))# True  all_nums:父  num4:子  .issuperset()父找子
     6 print(num4.issubset(all_nums))# True  all_nums:父  num4:子  .issubset()子找父
     7 # 可使用以下几种测试该方法
     8 # {1,2,3}
     9 # {a-z}
    10 # {A-Z}
    11 # {~=}

    2、验证密码是否合法,密码需包含大写字母、小写字母、数字、特殊字符

     1 import string
     2 all_nums = set(string.digits)
     3 lower = set(string.ascii_lowercase)
     4 upper = set(string.ascii_uppercase)
     5 punctuation = set(string.punctuation)
     6 for i in range(5):
     7     pwd = input('请输入你的密码:').strip()
     8     pwd = set(pwd)
     9     if pwd & all_nums and pwd & lower and pwd & upper and pwd & punctuation:
    10         print('密码校验合法')
    11     else:
    12         print('密码不合法,必须包含大写字母、小写字母、数字、特殊字符!')

     3、使用交集判断密码是否合法

    import string
    
    password = 'abc123A'
    
    password_set = set(password)
    
    if password_set & set(string.digits) and password_set & set(string.ascii_lowercase) 
        and password_set & set(string.ascii_uppercase):
        print('密码合法')
    else:
        print('不合法')
  • 相关阅读:
    把影响集中到一个点
    How to avoid Over-fitting using Regularization?
    适定性问题
    Numerical Differentiation 数值微分
    What Every Computer Scientist Should Know About Floating-Point Arithmetic
    Generally a good method to avoid this is to randomly shuffle the data prior to each epoch of training.
    What is the difference between iterations and epochs in Convolution neural networks?
    Every norm is a convex function
    Moore-Penrose Matrix Inverse 摩尔-彭若斯广义逆 埃尔米特矩阵 Hermitian matrix
    perl 类里的函数调用其他类的函数
  • 原文地址:https://www.cnblogs.com/Noul/p/9163130.html
Copyright © 2011-2022 走看看