zoukankan      html  css  js  c++  java
  • 53.基础语法-列表

    list列表的特例

    • 用list()创建单个字符串列表时会打散字符串['s', 'd', 'f']
    print(list("sdf"))
    

    list列表的操作

    访问操作

    • 直接用下标操作,下标从0开始

    切片操作

    • 对列表的任意截取,截取后创建一个新列表,原列表不变
    • 切片下标时左包括右不包括
    • 超出下标时,不考虑多余下标内容,也不报错
    • 下标为正数时从左往右截取,下标从0开始
    • 下标为负数时从右往左截取,下标从-1开始,依然时左包括右不包括
    l_qp = [1, 2, 3, 4, 5]
    print(l_qp)
    print(l_qp[0:])
    print(l_qp[:3])
    print(l_qp[::2])
    print(l_qp[2:(4+1)])
    print(l_qp[2:100])
    print(l_qp[4:2:-1])
    print(l_qp[-3:-1])
    

    列表的增删改查复制

    • 增:拼接,后面加,插入
    l_z = [1, 2, 3, 4, 5]
    print(l_z + [6, 7])
    print(l_z * 2)
    l_z.extend([10, 11])
    print(l_z)
    l_z.append(8)
    print(l_z)
    l_z.insert(1, 9)
    print(l_z)
    
    • 删:del,.pop(),.remove(),.clear()
    l_s = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    del l_s[2],l_s[1]
    print(l_s)
    l_s.pop(1)
    print(l_s)
    l_s.remove(9)
    print(l_s)
    l_s.clear()
    print(l_s)
    
      • 直接修改列表中的元素即可
      • 前一讲有讲查找
    • 复制
      • = 列表直接像变量一样赋值复制后,两个列表地址一样
      • .copy()使用此函数后时生成一个新的列表
    l_f0 = [1, 2, 3, 4, 5]
    l_f1 = l_f0
    print(id(l_f0))
    print(id(l_f1))
    l_f2 = l_f0.copy()
    print(id(l_f0))
    print(id(l_f2))
    
  • 相关阅读:
    WPF Get jiayuan outbox list(send mail box)
    Python中列表的各种方法
    Python中字符串拼接的三种方式
    Python2中input()、raw_input()和Python3中input()
    Python 中for...esle和while...else语法
    第 20 章 设置应用程序的样式并对其进行部署
    第 19 章 用户帐号
    第 18 章 Django 入门
    第 17 章 使用API
    安装requests
  • 原文地址:https://www.cnblogs.com/TK-tank/p/12345338.html
Copyright © 2011-2022 走看看