python 列表

发布时间:2019-03-06 17:04:56编辑:auto阅读(2091)

      1.列表

    1.创建列表方式

    1.1用中括号

    >>> number = [1,2,3,4,5]
    >>> number
    [1, 2, 3, 4, 5]
    1.2.list()方法,参数应为可迭代序列

    >>> list((1,2,3,4))
    [1, 2, 3, 4]
    1.3.创建空列表

    >>> emtry = []

    2.列表的操作

    2.1:添加元素

    2.1.1:  append()  ,向列表末尾添加一个对象

    >>> number = [1,2,3,4]
    >>> number.append(5)
    >>> number
    [1, 2, 3, 4, 5]
    View Code

     

    2.1.2:extend(),两个列表组合成一个列表

    >>> name = ['zzh','ssc','cch']
    >>> name.extend(['ee','cc','gg'])
    >>> name
    ['zzh', 'ssc', 'cch', 'ee', 'cc', 'gg']
    View Code

    2.1.3:insert(),向列表中指定位置添加元素

    >>> ls = ['cd','dir']
    >>> ls.insert(1,'chown')
    >>> ls
    ['cd', 'chown', 'dir']
    View Code

    2.2:查找列表中的元素

    2.2.1:通过索引

    >>> ls = ['cd','dir']
    >>> ls[0]
    'cd'
    View Code

    2.2.2:通过切片

    >>> ls = ['cd','dir','chown','mkdir','ll','tail']
    >>> ls[:]   #查找所有的元素
    ['cd', 'dir', 'chown', 'mkdir', 'll', 'tail']
    >>> ls[1:]  #查找从索引1以后的元素
    ['dir', 'chown', 'mkdir', 'll', 'tail']
    >>> ls[:3]  #查找从索引3以前的元素
    ['cd', 'dir', 'chown']
    >>> ls[1:4] # 查找从索引1到4(不包含4)的元素
    ['dir', 'chown', 'mkdir']
    >>> ls[-1]  #逆向查询
    'tail'
    >>> 
    View Code

    2.2.3:切片可根据步长实现功能

    >>> eggs = ['鸡蛋', '鹅蛋', '鸭蛋', '鹌鹑蛋']
    >>> eggs[0:4:2]
    ['鸡蛋', '鸭蛋']
    View Code

    2.2.4:通过切片实现列表元素变更位置

    >>> eggs = ['鸡蛋','鸭蛋','鹅蛋','鹌鹑蛋']
    >>> eggs[1] = '鹅蛋'
    >>> eggs[2] = '鸭蛋'
    >>> eggs
    ['鸡蛋', '鹅蛋', '鸭蛋', '鹌鹑蛋']
    View Code

    2.3:删除元素

    2.3.1:通过索引,删除元素

    >>> eggs = ['鸡蛋', '鹅蛋', '鸭蛋', '鹌鹑蛋']
    >>> del eggs[0]
    >>> eggs
    ['鹅蛋', '鸭蛋', '鹌鹑蛋']
    View Code

    2.3.2:直接删除整个列表

    >>> eggs = ['鸡蛋', '鹅蛋', '鸭蛋', '鹌鹑蛋']
    >>> del eggs
    >>> eggs
    Traceback (most recent call last):
      File "<pyshell#43>", line 1, in <module>
        eggs
    NameError: name 'eggs' is not defined
    View Code

    2.3.3:pop()方法

    >>> eggs = ['鸡蛋', '鹅蛋', '鸭蛋', '鹌鹑蛋']
    >>> eggs.pop() #删除最后一个参数并返回
    '鹌鹑蛋'
    >>> eggs
    ['鸡蛋', '鹅蛋', '鸭蛋']
    View Code
    >>> eggs = ['鸡蛋', '鹅蛋', '鸭蛋', '鹌鹑蛋']
    >>> eggs.pop(1)  #删除指定索引的元素并返回
    '鹅蛋'
    >>> eggs
    ['鸡蛋', '鸭蛋', '鹌鹑蛋']
    View Code

    3.一些常用的操作符

    3.1:比较符

    >>> num1 = [1,2,3]
    >>> num2 = [2,3]
    >>> num1 > num2  #列表包含多个元素的时候,默认是从第一个元素开始比较,只有一个PK赢了,就算整个列表赢了
    False
    View Code

     3.2:加号,把两个列表合并成一个列表

    >>> num1 = [1,2,3]
    >>> name = ['zs','ls','ww']
    >>> num1 + name
    [1, 2, 3, 'zs', 'ls', 'ww']
    View Code

    3.3:乘法,复制自身若干次

    >>> name = ['zs','ls','ww']
    >>> name * 4
    ['zs', 'ls', 'ww', 'zs', 'ls', 'ww', 'zs', 'ls', 'ww', 'zs', 'ls', 'ww']
    View Code

    3.4:成员关系符in或not in

    >>> ll = [1, 2, 3, 'zs', 'ls', 'ww']
    >>> 'zs' in ll
    True
    >>> 'cd' in ll
    False
    View Code

    4.列表的方法

    4.1:count() 这个方法的作用是计算的参数在列表中出现的次数

    >>> ll = [1,1,2,3,4,5,6,14,21]
    >>> ll.count(1)
    2
    View Code

    4.2:index() 这个方法会返回它的参数在列表中的位置

    >>> ll = [1,1,2,3,4,5,6,14,21]
    >>> ll.index(1)  #有相同的时候,显示最先的位置
    0
    >>> ll.index(4)
    View Code

    4.3:reverse() 将整个列表原地翻转

    >>> ll = [1,1,2,3,4,5,6,14,21]
    >>> ll.reverse()
    >>> ll
    [21, 14, 6, 5, 4, 3, 2, 1, 1]
    View Code

    4.4:sort() 指定的方式对列表的成员进行排序,默认不需要参数,从小到大排列

    >>> l2 = [8,9,3,5,2,6,10,1,0]
    >>> l2.sort()
    >>> l2
    [0, 1, 2, 3, 5, 6, 8, 9, 10]
    View Code

    4.5:当想要从大到小排列时,可以先用sort()方法,在用revese()方法,也可以用sort(reverse=True)

    >>> ll = [8,9,3,5,2,6,10,1,0]
    >>> ll.sort()
    >>> ll.reverse()
    >>> ll
    [10, 9, 8, 6, 5, 3, 2, 1, 0]
    >>> lll = [8,9,3,5,2,6,10,1,0]
    >>> lll.sort(reverse=True)
    >>> lll
    [10, 9, 8, 6, 5, 3, 2, 1, 0]
    View Code

    4.6:分片拷贝

    >>> li1 = [1,3,2,9,7,8]
    >>> li2 = li1[:]  #不随li1的变化而变化
    >>> li3 = li1
    >>> li1.sort()
    >>> li1
    [1, 2, 3, 7, 8, 9]
    >>> li3
    [1, 2, 3, 7, 8, 9]
    >>> li2
    [1, 3, 2, 9, 7, 8]  
    View Code

     

关键字