python之LIST基础操作

发布时间:2019-06-24 15:53:34编辑:auto阅读(1183)

    1,创建列表

    >>> list1=['a','b','c','d']
    >>> list2=[1,2,3,4]
    >>> list3=['a','b','c',1,2,3]

    2,访问列表中的值

    >>> print 'list1[0]:',list1[0]
    list1[0]: a
    >>> print 'list2[2]:',list2[2]
    list2[2]: 3

    负数索引值计算公式list[-n]==list[len(list)-n]

    >>> list1[-2]==list1[len(list1)-2]
    True
    >>>

     

    3修改或更新列表

    >>> print 'list1[1]:',list1[1]
    list1[1]: b
    >>> list1[1]=3
    >>> print 'list1[1]:',list1[1]
    list1[1]: 3
    >>>

    4,删除列表元素

    >>> list1
    ['a', 3, 'c', 'd']
    >>> del list1[1]
    >>> list1
    ['a', 'c', 'd']

    5,截断获取list

    >>> list1
    [1, 2, 3, 4, 5, 6, 7]
    >>> list1[1:4]
    [2, 3, 4]
    >>> list1[1:-1]
    [2, 3, 4, 5, 6]
    >>> list1[1:]
    [2, 3, 4, 5, 6, 7]
    >>> list1[:]
    [1, 2, 3, 4, 5, 6, 7]

    1、从1开始但不包含3List

    2、从list的第二个元素到list最后一个元素之间的,但不包括最后一个

     

    3、按下标从03的,但不包括3

     

          效果同上,0可以省略

     

    4、从下标n到数组最后一个,包括最后一个

     

    5list的全部元素

    6,添加元素

    >>> list1
    [1, 2, 3, 4, 5, 6, 7]
    >>> list1+=['a','b']
    >>> list1
    [1, 2, 3, 4, 5, 6, 7, 'a', 'b']
    >>>list1.append("hello")
    >>> list1
    [1, 2, 3, 4, 5, 6, 7, 'a', 'b', 'hello']
    >>> list1.append([1,2])
    >>> list1
    [1, 2, 3, 4, 5, 6, 7, 'a', 'b', 'hello',[1, 2]]
    >>>list1.extend(['one','two','three'])
    >>> list1
    [1, 2, 3, 4, 5, 6, 7, 'a', 'b', 'hello',[1, 2], 'one', 'two', 'three']
    >>> list1.extend('jia')
    >>> list1
    [1, 2, 3, 4, 5, 6, 7, 'a', 'b', 'hello',[1, 2], 'one', 'two', 'three', 'j', 'i', 'a']
    >>> list1.insert(0,'a')
    >>> list1.insert(2,'b')
    >>> list1
    ['a', 1, 'b', 2, 3, 4, 5, 6, 7, 'a', 'b','hello', [1, 2], 'one', 'two', 'three', 'j', 'i', 'a']
    >>>

     

    1、使用+号操作符连接list创建新的listlist没有数量限制,但这种操作实际是创建了2list对内存消耗比较大

    2list可以装载任意数据类型的元素,不必是同一类型

     

    3append()方法可以将一个元素(任意数据类型)添加到list末尾

     

    4List实际是个类,创建一个List实际上是实例化一个类,extend()方法接收一个list作为参数并把新list的每个元素添加到原有的list

     

    5insert()方法添加一个元素到list中,第一个参数是添加到list的位置的索引值

          list的元素可以重复

    7list值操作

    >>> list1
    ['a', 1, 'b', 2, 3]
    >>> list1.count()
    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
    TypeError: count() takes exactly oneargument (0 given)
    >>> list1.count('b')
    1
    >>> list1.count(2)
    1
    >>> 'b' in list1
    True
    >>> 4 in list1
    False
    >>> list1.index(3)
    4
    >>> list1.index('3')
    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
    ValueError: list.index(x): x not in list
    >>> list1.index('b')
    2
    >>> list1.index('d')
    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
    ValueError: list.index(x): x not in list
    >>>

    1count()方法返回查找值在list中的数量

    2、如果只想知道是否包含查找的值可以使用in,返回True False,这种方法要比count()方法速度快

     

    3、使用index()方法返回值所在的索引值,也可以添加第二个参数作为查找开始位置,第三个参数作为查找结束为止

     

    4index()方法只返回第一次查找到的位置

     

    5、因为-1Python中是有意义的索引值,所以index()方法没有找到对应的位置会抛异常

    8,删除

    Remove删除

    >>> list1
    ['a', 1, 'b', 2, 3]
    >>> list1.remove('b')
    >>> list1
    ['a', 1, 2, 3]
    >>> list1.remove('b')
    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
    ValueError: list.remove(x): x not in list
    >>>

    1、使用remove()方法,remove方法接受一个值,并将第一个遇到的删除,同样下标无间隙

    2、使用remove方法未能移除一个值时会抛异常

    使用pop方法移除

    >>> list1
    ['a', 1, 2, 3]
    >>> list1.pop()
    3
    >>> list1
    ['a', 1, 2]
    >>> list1.pop()
    2
    >>> list1.pop()
    1
    >>> list1.pop()
    'a'
    >>> list1.pop()
    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
    IndexError: pop from empty list

    1、调用pop()方法不给参数,将默认删除list的最后一个元素

    2、调用pop()方法并传一个索引值,将会删除特定位置的值

     

    3、在空的list调用pop()方法会抛异常

     


关键字