zoukankan      html  css  js  c++  java
  • Python3-笔记-B-001-数据结构-列表list[ ]

    # 列表[有序可变序列]
    def lists():
    # --- 列表 ---
    # 列表可通过 append() / pop() 方法,作为栈使用
    # 列表可通过 deque() 封装,作为双向队列使用
    # 创建
    lists = ["a", "b", "c"] # 列表
    lists = list() # 空列表
    lists = list((1, 2, 3)) # (将 元组 转为列表 (:将字典转为列表会损失数据)
    lists = [i for i in range(10)] # 其他:[(10,i) for i in range(10)]

    # 统计
    length = len(lists) # 列表长度
    count = lists.count("c") # 统计该元素数量

    # 获取
    str0 = lists[0] # 取出列表中的数据
    lists = lists[0:3]
    lists = lists[0:3:2]
    ary2 = lists.copy() # 浅拷贝列表

    # 查找
    lists = ["a", "b", "c", 'b', 'a', 'c', 'd', 'b', 'a', 'b'] # 列表
    index = lists.index("b") # 查询该元素的位置, 不存在抛异常(注意) => 1
    index = lists.index("b", 2) # 起始位置,默认是0 =>3
    index = lists.index("b", 4, 8) # 终止位置,默认是列表长度 =>7
    elem = min(lists)
    elem = max(lists)

    # 遍历
    for i in lists:
    print("for:%s" % i)

    # 修改
    lists[0] = "0" # 修改第一个数据
    lists.reverse() # 倒序
    lists.sort() # (自然顺序)排序

    # 添加
    lists.append("d") # 添加数据(加到末尾)
    lists.insert(1, "a1") # 插入数据
    lists.extend(["d", "e", "f"]) # 添加数据(元素追加到末尾)
    lists = [1, 2] + [3, 4] # 合并列表
    lists = [1, 2] * 3 # 添加元素到自身3

    # 删除
    del lists[1] # 根据索引删除(注意)
    lists.pop() # 删除最后元素
    lists.remove(1) # 根据元素删除, 找不到则抛一行
    lists.clear() # 清空列表

    # 判断
    boolean = "a" in lists # 该元素是否存在于列表中
    boolean = "a" not in lists # 该元素是否不存在于列表中

    # 排序
    lists = sorted(lists) # 字典(无序可变的映射类型,键值对集合,且键唯一)
  • 相关阅读:
    uva 11248 最大流 ISAP
    【力扣】133. 克隆图
    【力扣】125. 验证回文串
    【力扣】130. 被围绕的区域
    【力扣】337. 打家劫舍 III
    【力扣】104. 二叉树的最大深度-及二叉树的遍历方式
    【力扣】392. 判断子序列
    【力扣】95. 不同的二叉搜索树 II
    【力扣】120. 三角形最小路径和
    【力扣】两个数组的交集 II
  • 原文地址:https://www.cnblogs.com/vito13/p/7729944.html
Copyright © 2011-2022 走看看