列表方法
=
Python 3.5.2 (default, Sep 14 2016, 11:27:58) [GCC 6.2.1 20160901 (Red Hat 6.2.1-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> L=['good','boy','!'] >>> L ['good', 'boy', '!'] >>> L. L.__add__( L.__format__( L.__init__( L.__reduce__( L.__str__( L.insert( L.__class__( L.__ge__( L.__iter__( L.__reduce_ex__( L.__subclasshook__( L.pop( L.__contains__( L.__getattribute__( L.__le__( L.__repr__( L.append( L.remove( L.__delattr__( L.__getitem__( L.__len__( L.__reversed__( L.clear( L.reverse( L.__delitem__( L.__gt__( L.__lt__( L.__rmul__( L.copy( L.sort( L.__dir__( L.__hash__ L.__mul__( L.__setattr__( L.count( L.__doc__ L.__iadd__( L.__ne__( L.__setitem__( L.extend( L.__eq__( L.__imul__( L.__new__( L.__sizeof__( L.index(
1、append
append方法用于在列表末尾追加新的对象:
>>> L=['index0','index1','index2'] >>> L ['index0', 'index1', 'index2'] >>> L.append('index3') >>> L ['index0', 'index1', 'index2', 'index3'] >>> L.append([1,2,3]) >>> L ['index0', 'index1', 'index2', 'index3', [1, 2, 3]] >>> L.append(('Hello','World','!')) >>> L ['index0', 'index1', 'index2', 'index3', [1, 2, 3], ('Hello', 'World', '!')] >>> L.append('index5') >>> L ['index0', 'index1', 'index2', 'index3', [1, 2, 3], ('Hello', 'World', '!'), 'index5'] >>> len(L) 7 >>> L.append() Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> L.append() TypeError: append() takes exactly one argument (0 given) >>> L.append(None) >>> L ['index0', 'index1', 'index2', 'index3', [1, 2, 3], ('Hello', 'World', '!'), 'index5', None] >>> L.append([]) >>> L ['index0', 'index1', 'index2', 'index3', [1, 2, 3], ('Hello', 'World', '!'), 'index5', None, []] >>> id(L) 19215544 >>> L.append(1231) >>> L ['index0', 'index1', 'index2', 'index3', [1, 2, 3], ('Hello', 'World', '!'), 'index5', None, [], 1231] >>> id(L) 19215544 >>>
2、count
count方法统计某个元素在列表中出现的次数:
>>> help(L.count) Help on built-in function count: count(...) L.count(value) -> integer -- return number of occurrences of value >>> L=['to','be','or','not','to','be'] >>> L ['to', 'be', 'or', 'not', 'to', 'be'] >>> L.count('to') 2 >>> x=[[1,2],1,1,[1,2,1,[1,2]]] >>> x.count(1) 2>>> x.count([1,2]) 1 >>>
3、extend
官方说明:
>>> help(L.extend) Help on built-in function extend: extend(...) L.extend(iterable) -> None -- extend list by appending elements from the iterable
extend方法可以在列表的末尾一次性追加别一个序列中的多个值;换句话说,可以用新列表扩展原有的列表:
>>> a=[1,2,3,4] >>> id(a) 20761256 >>> b=[5,6,7,8,9] >>> id(b) 20760536 >>> c=a.extend(b) >>> c >>> print(c) None >>> a.extend(b) >>> a [1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9] >>> b [5, 6, 7, 8, 9] >>> id(a) 20761256 >>> b.extend(a) >>> b [5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9] >>> >>> a [1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9] >>> b [5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9] >>> a.extend(b) >>> a [1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9]
4、index
index方法用于从列表中找出某个值第一个匹配项的索引位置:
举例:
>>> s='He who commences many things finishes but a few' >>> list1=s.split() >>> list1 ['He', 'who', 'commences', 'many', 'things', 'finishes', 'but', 'a', 'few'] >>> list1.index('who') 1 >>> list1.index('Few') Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> list1.index('Few') ValueError: 'Few' is not in list >>> list1.index('who',2) Traceback (most recent call last): File "<pyshell#17>", line 1, in <module> list1.index('who',2) ValueError: 'who' is not in list >>> list1.index('but',2) 6 >>> list1.index('things',4,5) 4 >>> list1.index('things',5,len(list1)) Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> list1.index('things',5,len(list1)) ValueError: 'things' is not in list >>> list1.index('things',3,len(list1)) 4 >>> list1.index('a',-2,-1) 7 >>> len(list1) 9 >>>
5、insert
insert方法用于将对象插入到列表中;
>>> list1 ['He', 'commences', 'many', 'things', 'finishes', 'but', 'a', 'few'] >>> list1.insert(1,'who') >>> list1 ['He', 'who', 'commences', 'many', 'things', 'finishes', 'but', 'a', 'few'] >>> list1.insert(0,'who') >>> list1 ['who', 'He', 'who', 'commences', 'many', 'things', 'finishes', 'but', 'a', 'few'] >>>
6、pop
pop方法会移除列表中的一个元素(默认是最后一个),并且返回该元素的值:
>>> x=[1,2,3,4] >>> x.pop() 4 >>> x [1, 2, 3] >>> x.pop(0) 1 >>> x [2, 3] >>> x.pop(1) 3 >>> x [2] >>> help(x.pop) Help on built-in function pop: 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. >>>
注意: pop方法是唯一一个既能修改列表又返回元素(除了None)的列表方法。
7、remove
remove方法用于移除列表中某个值的第一个匹配项:
>>> x ['to', 'be', 'or', 'not', 'to', 'be'] >>> x.remove('be') >>> x ['to', 'or', 'not', 'to', 'be'] >>> print(x.remove('bee')) Traceback (most recent call last): File "<pyshell#65>", line 1, in <module> print(x.remove('bee')) ValueError: list.remove(x): x not in list >>> print(x.remove('to')) None >>> x ['or', 'not', 'to', 'be'] >>> x.insert(0,'to') >>> x ['to', 'or', 'not', 'to', 'be'] >>> x.remove() Traceback (most recent call last): File "<pyshell#70>", line 1, in <module> x.remove() TypeError: remove() takes exactly one argument (0 given) >>>
8、reverse
reverse方法将列表中的元素反向存放;
>>> help(list.reverse) Help on method_descriptor: reverse(...) L.reverse() -- reverse *IN PLACE* >>> x=[1,2,3,4] >>> x [1, 2, 3, 4] >>> x.reverse() >>> x [4, 3, 2, 1] >>> x.reverse() >>> x [1, 2, 3, 4] >>>
9、sort
sort方法用于在原位置对列表进行排序,在‘原位置排序’意味着改变原来的列表,从而让其中的元素能按一定的顺序排列,而不是简单地返回一个已经排序
的列表的副本;
>>> x=[2,55,123,89,-2,23] >>> x.sort() >>> x [-2, 2, 23, 55, 89, 123] >>> x.sort() >>> x [-2, 2, 23, 55, 89, 123]
sort方法修改原列表并返回了空值(None):
示例:
>>> help(list.sort) Help on method_descriptor: sort(...) L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* >>> x=[23,44,1,5,-100] >>> y=x.sort() #千万不要这样做! >>> x [-100, 1, 5, 23, 44] >>> y >>> print(y) None >>>
那么当用户需要一个排好序的列表副本,同时又保留原来列表不变的时候,问题就出现了,为了实现这个功能的正解方法是,首先把x的副本赋值给y,然后对y进行排序,
>>> x=[23,12,9,4,1231] >>> x [23, 12, 9, 4, 1231] >>> y=x[:] >>> y [23, 12, 9, 4, 1231] >>> id(x) 20712184 >>> id(y) 20711584 >>> z=x >>> id(z) 20712184 >>> y.sort() >>> x [23, 12, 9, 4, 1231] >>> y [4, 9, 12, 23, 1231] >>>
再次调用x[:]得到的是包含了x所有元素的分片,这是一种很有效率的复制整个列表的方法
假如只是简单把x赋值给y是没用的,因为这样做让x和y都指向同一个列表了。