美文网首页
python基础--列表list

python基础--列表list

作者: fada492daf5b | 来源:发表于2018-01-22 07:54 被阅读0次

    1. 作用

    python中的list类似数组,python最常见的数据类型,可以存储不同类型的数据。

    2. 操作

    创建列表

    >>> l1 = ['Tommy', 'Laura', 'Andy']
    >>> l1
    ['Tommy', 'Laura', 'Andy']
    >>> l2 = [i for i in range(10)] 
    >>> l2
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> l3 = ['Bill', 25, 'Teacher']
    >>> l3
    ['Bill', 25, 'Teacher']
    >>> 
    

    利用索引进行访问,修改

    >>> l1[1]
    'Laura'
    >>> l2[0]
    0
    >>> l1[1] = 'Maria'
    >>> l1
    ['Tommy', 'Maria', 'Andy']
    >>> l2[0] = 100
    >>> l2
    [100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> 
    

    列表添加元素append,插入元素insert

    >>> l1
    ['Tommy', 'Maria', 'Andy']
    >>> l1.append('Julia')
    >>> l1
    ['Tommy', 'Maria', 'Andy', 'Julia']
    >>> l2.append(l0)
    >>> l2
    [100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> l1.append(l2)
    >>> l1
    ['Tommy', 'Maria', 'Andy', 'Julia', [100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
    >>> l2.insert(2, 20)
    [100, 1, 20, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> 
    

    删除列表元素del,pop, remove, clear

    >>> l3
    ['Bill', 25, 'Teacher']
    >>> del l3[2] # 删除某一个元素
    >>> l3
    ['Bill', 25]
    >>> l3.pop() # 删除最后一个元素,也可以添加参数l3.pop(2)进行删除并返回
    25
    >>> l3
    ['Bill']
    >>> l3.remove('Bill') # 删除指定元素
    >>> l3
    []
    >>> l3 = ['Bill', 25, 'Teacher']
    >>> l3
    ['Bill', 25, 'Teacher']
    >>> l3.clear()
    >>> l3
    []
    >>>
    

    利用切片截取列表[n:m]

    >>> l2
    [100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> l2[2:6]
    [2, 3, 4, 5]
    >>> 
    

    列表合并extend

    >>> a = [1, 2, 3]
    >>> b = [4, 5, 6]
    >>> a.extend(b)
    >>> a
    [1, 2, 3, 4, 5, 6]
    >>> 
    

    找出某元素的索引index

    >>> l1
    ['Tommy', 'Maria', 'Andy', 'Julia', [100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
    >>> l1.index('Tommy')
    0
    >>> 
    

    反向列表中元素reverse, [::-1]

    >>> l2.reverse()
    >>> l2
    [10, 9, 8, 7, 6, 5, 4, 3, 2, 20, 1, 100]
    >>> l2[::-1]
    [100, 1, 20, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> 
    

    列表排序sort([func])

    >>> list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
    >>> list1.sort()
    >>> list1
    ['Baidu', 'Google', 'Runoob', 'Taobao']
    

    列表复制copy

    >>> list3 = [x for x in range(10)]
    >>> list1 = list3.copy() # 深拷贝
    >>> list1
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list1[2] = 100 
    >>> list3
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list1
    [0, 1, 100, 3, 4, 5, 6, 7, 8, 9]
    >>> list2 = list1 # 浅拷贝
    >>> list2
    [0, 1, 100, 3, 4, 5, 6, 7, 8, 9]
    >>> list1[2] = 2
    >>> list1
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list2
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>>
    

    列表长度len,最大值max

    >>> l1 = [x for x in range(5)]
    >>> len(l1)
    5
    >>> max(l1)
    4
    >>>min(l1)
    0 
    

    将元组转换为列表list

    >>> tup = (1, 2, 3)
    (1, 2, 3)
    >>> list(tup)
    [1, 2, 3]
    

    相关文章

      网友评论

          本文标题:python基础--列表list

          本文链接:https://www.haomeiwen.com/subject/exvkaxtx.html