zoukankan      html  css  js  c++  java
  • Python列表操作与深浅拷贝(6)——列表索引、查询、修改、扩展

    列表list定义

    L1 = []
    L2 = [1,2,'abc']
    L3 = list()
    L4 = list(range(5))
    L5 = [1,'abc',True,None,[4,5,'abc'],str]
    isinstance(L5,list)
    True

    列表索引访问

    L6 = list(range(5,10))
    L6
    [5, 6, 7, 8, 9]

    正索引:从下边界至上边界,从0开始,为列表中每一个元素编号  #为便于理解,认为列表左为下边界,右为上边界

    负索引:从上边界至下边界,从-1开始

    列表通过索引访问:list[index]  #index为索引

    L6[3],L6[-3]
    (8, 7)

    正负索引不可以超出边界,否则报异常IndeError

    L6[9]
    ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
    <ipython-input-29-fba6b584fe36> in <module>
    ----> 1 L6[9]
    
    IndexError: list index out of range

    列表元素查询

    index(value,[start,[stop]]) 通过值value,从指定区间查找列表内的元素是否匹配;时间复杂度为O(n)

    从下边界开始,匹配第一个就立即返回索引

    匹配不到报异常ValueError

    L6.index(5)
    0
    
    L6.index(
    5,2,4) 2
    L6.index(
    9) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-41-85d63fc6c2a2> in <module> ----> 1 L6.index(9) ValueError: 9 is not in list

    count(value) 返回列表中匹配value次数;时间复杂度为O(n)

    L6.count(5)
    2

    len() 统计列表长度;时间复杂度为O(1)

    len(L6)
    5

    时间复杂度

    O(1) 执行步骤为常数,效率较高

    O(n) 随着列表数据规模的增大,执行步骤增大,效率低

    修改,增加,插入元素

    list[index]=value 列表元素修改;时间复杂度为O(1)

    L6[-3] = 5
    L6
    [5, 6, 5, 8, 9]
    
    L6[-1] = L6[-3] + 5
    L6
    [5, 6, 5, 8, 10]

    append(object) 尾部追加,返回值为None,修改自身;时间复杂度为O(1)

    L7 = list(range(5,10))
    L7
    [5, 6, 7, 8, 9]
    
    L7.append(12)
    L7
    [5, 6, 7, 8, 9, 12]

    insert(index,object) 指定index处插入object,返回值为None,修改自身;时间复杂度为O(n)

    L7.insert(1,12)
    L7
    [5, 12, 6, 7, 8, 9, 12]

    extend(iteratable) 追加可迭代对象的元素,返回值为None,修改自身;时间复杂度为O(1)

    L8 = list(range(1,4))
    L8
    [1, 2, 3]
    
    L7.extend(L8)
    L7
    [5, 12, 6, 7, 8, 9, 12, 1, 2, 3]
    
    L7.extend(range(15,19))
    L7
    [5, 12, 6, 7, 8, 9, 12, 1, 2, 3, 15, 16, 17, 18]

    + 连接操作,将两个列表连接,返回list,产生新列表,原列表不变

    L7,L8
    ([5, 12, 6, 7, 8, 9, 12, 1, 2, 3, 15, 16, 17, 18], [1, 2, 3])
    
    L7 + L8
    [5, 12, 6, 7, 8, 9, 12, 1, 2, 3, 15, 16, 17, 18, 1, 2, 3]
    
    L7,L8
    ([5, 12, 6, 7, 8, 9, 12, 1, 2, 3, 15, 16, 17, 18], [1, 2, 3])

    * 重复操作,将本列表元素重复n次,返回list,产生新列表,原列表不变

    L8 * 3
    [1, 2, 3, 1, 2, 3, 1, 2, 3]
    
    L8
    [1, 2, 3]
  • 相关阅读:
    go函数
    Linux 查看磁盘容量、查找大文件、查找大目录
    五分钟理解一致性哈希算法(consistent hashing)
    使用Java实现三个线程交替打印0-74
    Python实现IOC控制反转
    Wannafly挑战赛5 A珂朵莉与宇宙 前缀和+枚举平方数
    Yandex Big Data Essentials Week1 Scaling Distributed File System
    Yandex Big Data Essentials Week1 Unix Command Line Interface Processes managing
    Yandex Big Data Essentials Week1 Unix Command Line Interface File Content exploration
    Yandex Big Data Essentials Week1 Unix Command Line Interface File System exploration
  • 原文地址:https://www.cnblogs.com/omgasw/p/11609024.html
Copyright © 2011-2022 走看看