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) # 字典(无序可变的映射类型,键值对集合,且键唯一)
  • 相关阅读:
    css小随笔
    正则表达式的疑问
    笔记本各型号CPU性能比较
    调整Ajax的ValidatorCalloutExtender绑定后的提示字体
    GridView显示空表头
    VB.net检测输入内容
    asp.net中System.DateTime.Now.ToString()的一些用法
    收藏的手机论坛
    常用或者将要用的技巧或代码(网摘)
    使用Ajax的MaskedEditExtender来限制输入内容
  • 原文地址:https://www.cnblogs.com/vito13/p/7729944.html
Copyright © 2011-2022 走看看