• Learn_Day5 集合、三元运算、拷贝、函数


     集合


    list:允许重复的集合,可修改
    tuple:允许重复的集合,不可修改
    dict:允许重复的集合,可修改
    set:无序且不重复的元素集合

    创建:
    s = set()
    s = {11,22,33,44}

    转换:
    1 l1 = [11, 22, 11, 2]
    2 s2_l1 = set(l1)
    3 print("s2_l1", s2_l1)  # s2 {2, 11, 22}
    4 t1 = (11, 22, 11, 2)
    5 s2_t1 = set(t1)
    6 print("s2_t1", s2_t1)  # s2_t1 {2, 11, 22}
    集合==>>转换

    功能:se=set()    be=set()
    se.add    新增
    se = {11, 22, 33}
    se.add(44)
    print("add==>>", se)
    # add==>> {33, 11, 44, 22}
    # 新增
    clear    删除,清空
    se = {11, 22, 33}
    se.clear()
    print("clear==>>", se)
    # clear==>> set()
    # 清除
    se.difference(be)    找se中有,be中没有的集合,并赋值给新的变量
    se.difference_update(be)    删除se中和be中相同的元素,并更新se
    se = {11, 22, 33, 44}
    be = {22, 55}
    # se有,be没有
    ret = se.difference(be)
    print("se==>>", se, "difference==>>", ret)
    # se==>> {33, 11, 44, 22} difference==>> {33, 11, 44}
    # se有,be没有,更新se(删除相同的)
    ret = se.difference_update(be)
    print("se==>>", se, "difference_update==>>", ret)
    # se==>> {33, 11, 44} difference_update==>> None
    difference,difference_update
    se.discard    删除指定元素,没有不报错
    se.remove    删除指定元素,没有报错
    se = {11, 44, 77, 55} 
    be = {33, 77, 22, 55}
    
    # 删除指定元素,不存在不报错
    se.discard(55)
    print("discard==>>", se)
    # discard==>> {33, 11, 44, 77, 22}
    # 删除指定元素,不存在报错
    se.remove(55)
    print("remove", se)
    # KeyError: 55(报错)
    discard,remove
    se.intersection(be)    交集(找se和be中相同的元素)
    se.intersection_updat(be)    找se和be的交集,并更新到se中
     1 se = {11, 22, 33, 44}
     2 be = {22, 55}
     3 # 找交集
     4 ret = se.intersection(be)
     5 print("se==>>", se, "intersection==>>", ret)
     6 # se==>> {33, 11, 44, 22} intersection==>> {22}
     7 # 找到交集并更新到se
     8 ret = se.intersection_update(be)
     9 print("se==>>", se, "intersection_update==>>", ret)
    10 # se==>> {33, 11, 44, 22} intersection_update==>> None
    intersection,intersection_update
    se.isdisjoint(be)    判断是否无交集,有交集返回False,无交集返回True
    se = {11, 22, 33, 44}
    be = {22, 55}
    
    # 判断有无交集,有交集返回False,无交集返回True
    ret = se.isdisjoint(be)
    print("isdisjoint==>>", ret)
    # isdisjoint==>> False
    isdisjoint
    se.issubset(be)    判断se是否为be的子序列,是返回True,不是返回False
    se.issuperset(be)        判断se是否为be的父序列,是返回True,不是返回False
     1 se = {11, 22, 33, 44}
     2 be = {22, 55}
     3 
     4 # 判断se是否为be的子序列,是返回True,不是返回False
     5 ret = se.issubset(be)
     6 print("issubset==>>", ret)
     7 # issubset==>> False
     8 # 判断se是否为be的父序列,是返回True,不是返回False
     9 ret = se.issuperset(be)
    10 print("issuperst==>>", ret)
    11 # issuperst==>> False
    issubset,issuperset
    se.pop    移除元素并重新赋值(随机)
    se = {11, 22, 33, 44}
    # 移除元素并重新赋值(随机)
    ret = se.pop()
    print("se==>>", se, "pop==>>", ret)
    # se==>> {11, 44, 22} pop==>> 33
    pop
    se.symmetric_difference(be)    找se中有be中没有,be中有se中没有的集合,并赋值给新的变量
    se.symmetric_difference_update(be)    找se中有be中没有,be中有se中没有的集合,并更新se
    se = {11, 22, 33, 44}
    be = {22, 55, 77, 33}
    # 找se中有be中没有,找be中有se中没有,并赋值给新的变量
    ret = se.symmetric_difference(be)
    print("se==>>", se, "symmetric_difference==>>", ret)
    # se==>> {33, 11, 44, 22} symmetric_difference==>> {11, 44, 77, 55}
    # 找se中有be中没有,找be中有se中没有,并赋更新到se
    ret = se.symmetric_difference_update(be)
    print("se==>>", se, "symmetric_difference_update==>>", ret)
    # se==>> {11, 44, 77, 55} symmetric_difference_update==>> None
    symmetric_difference,symmetric_difference_update
    se.union(be)    并集
    se = {11, 22, 33, 44}
    be = {22, 55, 77, 33}
    # 并集
    ret = se.union(be)
    print("se==>>", se, "be==>>", be, "union==>>", ret)
    # se==>> {11, 44, 77, 55} be==>> {33, 77, 22, 55} union==>> {33, 11, 44, 77, 22, 55}
    # 并集
    se.update(be)    将be更新到se
    se = {11, 44, 77, 55} 
    be = {33, 77, 22, 55} 
    # 把be更新到se,be也可以是列表,元组
    ret = se.update(be)
    print("se==>>", se, "be==>>", be, "update==>>", ret)
    # se==>> {33, 11, 44, 77, 22, 55} be==>> {33, 77, 22, 55} update None
    update

    三元运算(三目运算)

    if 条件:
        pass
    else:
        pass
    等同于:
    值1 if 条件 else 值2
     1 # 1
     2 s1 = int(input("输入数字:"))
     3 if s1 == 1:
     4     name = "alex"
     5 else:
     6     name = "aric"
     7 print("1:", name)
     8 # 1: alex
     9 
    10 # 2
    11 name = "alex" if s1 == 1 else "aric"
    12 print("2:", name)
    13 # 2: alex
    14 
    15 # 将1简写成2,为三目运算(三元运算)
    三元运算

    深浅拷贝


    数字和字符串:赋值,深浅拷贝的地址永远一样
    import copy
    
    # 数字、字符串
    n1 = 123
    # n1 = "i am alex age 10"
    print(id(n1))
    # 赋值:id不变
    n2 = n1
    print(id(n2))
    # 浅拷贝:id不变
    n2 = copy.copy(n1)
    print(id(n2))
    # 深拷贝:id不变
    n3 = copy.deepcopy(n1)
    print(id(n3)
    # 数字、字符串
    其他数据类型:
        赋值:id不变
        浅拷贝:最外层id改变,里层id不变
        深拷贝:外出id改变,最里层id不变
     1 import copy
     2 
     3 # 其他数据类型
     4 n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
     5 # 赋值:id不变
     6 n2 = n1
     7 # 浅拷贝:最外层id改变,里层id不变
     8 n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
     9 n3 = copy.copy(n1)
    10 # 深拷贝:外层id改变,最里层id不变
    11 n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
    12 n4 = copy.deepcopy(n1)
    # 其他数据类型

    函数
    定义函数:
        def 函数名(形式参数):    
            def    关键字
            函数名    用于调用函数
            形式参数    用于给函数提供数据,定义的时候为形式参数,调用时为实际参数
            返回值    执行完毕后返回的数据,给调用者用作参考
    # def 函数名(参数):  函数名不能是中文
    #     函数体
    #     return 值  只要执行return,return下面的代码将不再执行
    View Code
        
    执行函数:
        函数名(实际参数)   
        执行时如果不按照顺序传参数,需要指定:
            例:def xxx(a, b, c):
                        …
                    xxx(b = xxx, c = xxx, a = xxx)
     
    默认参数:(放在参数最后)
     1     def xxx(a):
     2         aaa = a + "ooo"
     3         print(aaa)
     4         return aaa
     5     xxx("1")    ==>> 1ooo
     6 
     7     def xxx(a="b"):
     8         aaa = a + "ooo"
     9         print(aaa)
    10         return aaa
    11     xxx()       ==>> booo
     
    动态参数:
     1   def xxx(*args):
     2         return aaa
     3     xxx(11,22) ==>>(元组)
     4 
     5     def xxx(**kwargs):
     6         return aaa
     7     xxx(“k1”=11,”k2”=22) ==>>(字典)
     8 
     9     def xxx(*args,**kwargs): ==>>万能参数
    10         return aaa
    11     xxx(11,22,“k1”=11,”k2”=22) ==>> (元组,字典)
     
    全局变量、局部变量
    全局变量:所有人都能用(书写规范:大写)
    局部变量:一个人可以用(书写规范:小写)
     1 p = "alex"  # 全局变量
     2 
     3 def fun1():
     4     a = 123
     5     global p  # 指定p为全局变量
     6     p = "eric"  # 局部变量
     7     print(a)  # ==>> 123
     8 
     9 fun1()
    10 
    11 def fun2():
    12     a = 456
    13     print(p)  # ==>> alex  指定p后==>> eric
    14     print(a)  # ==>> 456
    15 
    16 fun2()
  • 相关阅读:
    GlusterFS + lagstash + elasticsearch + kibana 3 + redis日志收集存储系统部署 01
    Python输出字符串或文件颜色显示
    系统维护常用脚本
    免密码登录服务器python脚本
    python 常用模块之ConfigParser
    Robot Framework -004 为了进行Web测试,安装SeleniumLibrary外部库
    Robot Framework -005 测试SeleniumLibrary外部库,添加一个测试web用例
    Robot Framework -003 在Windows10 安装Eclipse作为编辑器,安装 RED 插件。
    Robot Framework -002 在Windows10上的安装
    Robot Framework -001 简介
  • 原文地址:https://www.cnblogs.com/grissom/p/6369584.html
Copyright © 2020-2023  润新知