2016.07.09-10 list、tuple


列表(list)
    初始化列表:
        lst = []
        lst = list()
        lst = [1, 2, 3]

    下标/索引操作:
        python中的索引从0开始
        lst[0]  取出第一个元素
        lst[-1] 负数索引表示从后往前,由-1开始,-1表示最后一个元素
        如果索引超出范围,将引发IndexError异常
        修改元素的时候,如果超出索引范围,也会引发IndexError异常

    增加元素:
        append方法:原地修改list,在list结尾追加一个元素,append方法的返回值是None
        |  append(...)
        |      L.append(object) -> None -- append object to end
        实例:
            >>> lst = [1, 2, 3]
            >>> lst.append(4)
            >>> lst
            [1, 2, 3, 4]
            >>>
        insert方法:在指定索引位置之前插入一个元素,insert操作的索引超出方位,如果是正索引,等效于append在列表最后追加,如果是负的索引,等效于insert(0, object)
        |  insert(...)
        |      L.insert(index, object) -- insert object before index
        实例:
            >>> lst.insert(0,0)
            >>> lst
            [0, 1, 2, 3, 4]
            >>> 
        extend方法:把一个可迭代对象扩展到列表
        |  extend(...)
        |      L.extend(iterable) -> None -- extend list by appending elements from the iterable
        实例:
            >>> lst
            [78, 0, 1, 2, 10, 99, 3, 4, 100]
            >>> l2 = [1,3,4,5,2]
            >>> lst.extend(l2)
            >>> lst
            lst
            >>> lst
            [78, 0, 1, 2, 10, 99, 3, 4, 100, 1, 3, 4, 5, 2]
            >>> 
    删除元素:(pop是针对的索引,remove针对的是值)
        pop方法:index默认为-1,删除-1所在的元素,并返回它,可以删除指定索引的元素,如果指定索引不存在则抛出IndexError异常
        |  pop(...)
        |      L.pop([index]) -> item -- remove and return item at index (default last).
        |      Raises IndexError if list is empty or index is out of range.
        实例:
            >>> lst
            [78, 0, 1, 2, 10, 99, 3, 4, 100, 1, 3, 4, 5, 2]
            >>> lst.pop()
            2
            >>> lst.pop(4)
            10
            >>> lst.pop(100)
            Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
            IndexError: pop index out of range
            >>> 

        remove方法:删除列表中第一个查找到的元素,如果值不存在,会抛出ValueError异常
        |  remove(...)
        |      L.remove(value) -> None -- remove first occurrence of value.
        |      Raises ValueError if the value is not present.
        实例:
            >>> lst
            [78, 0, 1, 2, 99, 3, 4, 100, 1, 3, 4, 5]
            >>> lst.remove(1)
            >>> lst
            [78, 0, 2, 99, 3, 4, 100, 1, 3, 4, 5]
            >>> lst.remove(7)
            Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
            ValueError: list.remove(x): x not in list
            >>> 
        clear方法,清空列表
        |  clear(...)
        |      L.clear() -> None -- remove all items from L
        实例:
            >>> lst
            [78, 0, 2, 99, 3, 4, 100, 1, 3, 4, 5]
            >>> lst.clear()
            >>> lst
            []
            >>> 
    查找/统计元素:
        index方法:查找指定元素在列表中第一次出现的索引,[start,[stop]]为可选参数,值为索引,用于指定查找范围。
        |  index(...)
        |      L.index(value, [start, [stop]]) -> integer -- return first index of value.
        |      Raises ValueError if the value is not present
        实例:
            >>> lst = [1, 2 ,3 ,4, 5, 1, 3]
            >>> lst.index(1)
            0
            >>> lst.index(1,3) 
            5
            >>> 
        count方法:统计指定元素在列表中出现的次数。
        |  count(...)
        |      L.count(value) -> integer -- return number of occurrences of value
        实例:
            >>> lst
            [1, 2, 3, 4, 5, 1, 3]
            >>> lst.count(1)
            2
            >>> 
        len函数:是一个内置函数,可用于统计列表的元素个数。
        len(obj, /)
        Return the number of items in a container.
        实例:
            >>> lst
            [1, 2, 3, 4, 5, 1, 3]
            >>> len(lst)
            7
            >>> 
    修改列表:
        sort方法:对列表进行排序,会原地修改列表,reverse为可选参数,默认为False,指定为True,列表逆序排序。
        |  sort(...)
        |      L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
        实例:
            >>>lst
            [1, 2, 3, 4, 5, 1, 3]
            >>> lst.sort()
            >>> lst
            [1, 1, 2, 3, 3, 4, 5]
            >>> lst.sort(reverse=True) 
            >>> lst
            [5, 4, 3, 3, 2, 1, 1]
        reverse方法:对列表进行反转,会原地修改列表。
        |  reverse(...)
        |      L.reverse() -- reverse *IN PLACE*
        实例:
            >>> lst = [5, 4, 3, 3, 2, 1, 1]
            >>> lst.reverse()
            >>> lst
            [1, 1, 2, 3, 3, 4, 5]
            >>> 
    其他方法:
        copy方法:复制列表,生成一个新的列表,浅拷贝。
        |  copy(...)
        |      L.copy() -> list -- a shallow copy of L
            >>> lst
            [1, 1, 2, 3, 3, 4, 5] 
            >>> lst2 = lst.copy()
            >>> lst2
            [1, 1, 2, 3, 3, 4, 5]
            >>> id(lst)
            140291324960712
            >>> id(lst2)
            140291337551944
            >>> 
        in,not in 成员操作符:检查元素是不是在列表中,返回一个布尔类型
            >>> lst
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
            >>> 10 in lst
            False
            >>> 2 in lst  
            True
            >>> 
        
    list切片赋值:如果所赋值的内容是可迭代的,会替换原来的元素(通常不会对切片进行赋值操作)
        >>> lst = list(range(0, 11))
        >>> lst
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        >>> lst[3:5] = ['x', 'y', 'z']
        >>> lst
        [0, 1, 2, 'x', 'y', 'z', 5, 6, 7, 8, 9, 10]
        >>> 

        >>> lst = list(range(0, 11))
        >>> lst[3: 5] = ['x']
        >>> lst
        [0, 1, 2, 'x', 5, 6, 7, 8, 9, 10]
        >>> 
        


元组(tuple):
    初始化元组:
        t = tuple([1, 2, 3])
        t = (1, 2, 3)
        t = (1, )
    元组是不可变的,这是元组和列表的最大区别。

    下标/索引操作:
        使用方法跟元组相同
    
    元组方法:
        由于元组是不可变类型,所以元组的方法就只有查找方法,countindex,用法跟list相同
原文地址:https://www.cnblogs.com/LouisZJ/p/5674506.html