• 第五章 列表、元组和字符串[DDT书本学习 小甲鱼]【4】


    5.1.9 关于分片“拷贝”的概念补充 【真正的拷贝是切片】
    代码
    list1=[1,3,2,9,7,8]
    list2=list1[:]
    print(list2)
    list3=list1
    print(list3)
    ----------------------
    [1, 3, 2, 9, 7, 8]
    [1, 3, 2, 9, 7, 8]

    以上代码看似一样的输出结果,事实上呢,作如下修改后
    list1 = [1, 3, 2, 9, 7, 8]
    list2 = list1[:]
    list3=list1
    #==============#
    list1.sort()
    print(list1)
    print(list2)
    print(list3)
    -------------------
    [1, 2, 3, 7, 8, 9]
    [1, 3, 2, 9, 7, 8] #切片是另外拷贝出来的,不变
    [1, 2, 3, 7, 8, 9] #Python里 变量似标签 一个数据贴了2个标签而已

    图片解析:“切片”和“贴标签”的不同之处


    5.2 元组:戴上了枷锁的列表
    元组是列表的表亲,最大区别是:列表可以改变,而元组不可以改变,创建
    元组的时候大多用小括号。
    5.2.1 创建和访问一个元组
    代码 访问元组和列表没什么差别
    tuple1=(1,2,3,4,5,6,7,8)
    print(tuple1)
    print(tuple[1])
    print(tuple[:5])
    print(tuple[5:])
    -------------------
    (1, 2, 3, 4, 5, 6, 7, 8)
    2
    (1, 2, 3, 4, 5)
    (6, 7, 8)

    也可以用分片的方式来复制一个元组
    tuple1=(1,2,3,4,5,6,7,8)
    tuple2=tuple1[:]
    print(tuple2)
    -----------------------
    (1, 2, 3, 4, 5, 6, 7, 8)
    试图去修改tuple1的元素--------出错了
    tuple1=(1,2,3,4,5,6,7,8)
    tuple1[1]=100
    print(tuple1)
    -----------------------------
    TypeError: 'tuple' object does not support item assignment

    开始实验:列表的标志是[],那么元组的标志是()么?不对,是逗号。证明如下
    代码
    temp=(1)
    print(temp)
    print(type(temp))
    ------------------------
    1
    <class 'int'>
    代码
    temp=1,2,3
    print(temp)
    print(type(temp))
    -------------------------
    (1, 2, 3)
    <class 'tuple'>
    发现没有?逗号才是关键,当然,如果创建一个空元组,可以用()
    temp=()
    print(type(temp))
    -----------------------
    <class 'tuple'>
    所以,创建元组,记得做上标记,逗号 这样他们都属于元组了
    temp=(1,)
    temp1=1,
    print(temp)
    print(temp1)
    ----------------------
    (1,)
    (1,)
    为了证明逗号的决定性作用,再举个例子
    a=8*(8)
    print(a)
    b=8*(8,)
    print(b)
    ----------------
    64
    (8, 8, 8, 8, 8, 8, 8, 8)
    ===============================未完待续 =================================

    Daodantou:“不积跬步,无以至千里.”
  • 相关阅读:
    appscan的初步使用
    解决selenium自动化上传图片或文件出现windows窗口问题
    写入excel数据报错:ValueError: Cannot convert {'code': 0, 'msg': 'login success!', 'username': 'test',} to Excel
    接口自动化使用session会话解决数据依赖问题
    使用django开发一个简单的post接口
    windows10,解决jmeter5.2.1版本界面字体过小问题
    django配置使用mysql数据库运行报错:django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'
    python装饰器
    python闭包
    工作总结
  • 原文地址:https://www.cnblogs.com/daodantou/p/10217617.html
Copyright © 2020-2023  润新知