Python:List (列表)
list 为Python内建类型,位于__builtin__模块中,元素类型可不同,元素可重复,以下通过实际操作来说明list的诸多功能,主要分为增、删、改、查
list帮助:在IDE中输入 help(list)可查看
Help on class list in module __builtin__: class list(object) | list() -> new empty list | list(iterable) -> new list initialized from iterable‘s items | | Methods defined here: | | __add__(...) | x.__add__(y) <==> x+y | | __contains__(...) | x.__contains__(y) <==> y in x | | __delitem__(...) | x.__delitem__(y) <==> del x[y] | | __delslice__(...) | x.__delslice__(i, j) <==> del x[i:j] | | Use of negative indices is not supported. | | __eq__(...) | x.__eq__(y) <==> x==y | | __ge__(...) | x.__ge__(y) <==> x>=y | | __getattribute__(...) | x.__getattribute__(‘name‘) <==> x.name | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __getslice__(...) | x.__getslice__(i, j) <==> x[i:j] | | Use of negative indices is not supported. | | __gt__(...) | x.__gt__(y) <==> x>y | | __iadd__(...) | x.__iadd__(y) <==> x+=y | | __imul__(...) | x.__imul__(y) <==> x*=y | | __init__(...) | x.__init__(...) initializes x; see help(type(x)) for signature | | __iter__(...) | x.__iter__() <==> iter(x) | | __le__(...) | x.__le__(y) <==> x<=y | | __len__(...) | x.__len__() <==> len(x) | | __lt__(...) | x.__lt__(y) <==> x x*n | | __ne__(...) | x.__ne__(y) <==> x!=y | | __repr__(...) | x.__repr__() <==> repr(x) | | __reversed__(...) | L.__reversed__() -- return a reverse iterator over the list | | __rmul__(...) | x.__rmul__(n) <==> n*x | | __setitem__(...) | x.__setitem__(i, y) <==> x[i]=y | | __setslice__(...) | x.__setslice__(i, j, y) <==> x[i:j]=y | | Use of negative indices is not supported. | | __sizeof__(...) | L.__sizeof__() -- size of L in memory, in bytes | | append(...) | L.append(object) -- append object to end | | count(...) | L.count(value) -> integer -- return number of occurrences of value | | extend(...) | L.extend(iterable) -- extend list by appending elements from the iterable | | index(...) | L.index(value, [start, [stop]]) -> integer -- return first index of value. | Raises ValueError if the value is not present. | | insert(...) | L.insert(index, object) -- insert object before index | | 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. | | remove(...) | L.remove(value) -- remove first occurrence of value. | Raises ValueError if the value is not present. | | reverse(...) | L.reverse() -- reverse *IN PLACE* | | sort(...) | L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; | cmp(x, y) -> -1, 0, 1 | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __hash__ = None | | __new__ = | T.__new__(S, ...) -> a new object with type S, a subtype of T
list帮助
1)增
a.初始化
直接用 list1 = list()或者 list1 =[ ]构造一个空列表
list2=[1,2,3] , list3=list(list2) 两种方式构造非空列表
b.追加元素
list2.append(5) --> [1,2,3,5],追加单个元素
list2.extend([‘a‘,‘b‘]) --> [1,2,3,5,‘a‘,‘b‘] 追加后面列表中所有元素
list2.insert(0,‘begin‘) --> [‘begin‘,1,2,3,5,‘a‘,‘b‘] 指定位置插入元素
2)删
list2.remove(‘a‘) --> [‘begin‘,1,2,3,5,‘b‘] 删除列表中指定值,只删除第一次出现的指定值,若指定值不在列表中则抛出ValueError
list2.pop(1) --> [‘begin‘,2,3,5,‘b‘] 删除指定索引处的值并返回,若列表为空或索引值超出范围则抛出IndexError,默认(无参情况下)删除最后一个元素并返回,利用list.pop()可实现栈操作
3)查
list2[0] --> 根据index查找元素 ,支持切片 ,如list[0:3] --> [‘begin‘,2,3]
‘begin‘ in / not in list2 --> 查看某元素是否在列表中,返回布尔值
list2.count(‘b‘) --> 返回某元素在列表中出现次数
4) 改
list[0]=‘end‘ --> 替换指定index索引处元素值
5) 其他操作
反转 ,list2.reverse() --> 将list2 元素顺序倒转
排序,list2.sort() --> 将list2 元素进行升序排序,或者用sorted(list2)进行排序
实例:
1、用list实现栈
栈原则,先进后出FILO。
1 #! /usr/bin/env python 2 # -*-coding:utf-8-*- 3 4 class Stack(object): 5 6 def __init__(self): 7 self.st = list() 8 9 def in_stack(self,x): 10 self.st.append(x) 11 print ‘element %s add to stack...‘% x 12 13 def out_stack(self): 14 print ‘element get out of stack...‘ 15 print self.st.pop() 16 17 def get_top(self): 18 print self.st[len(st)-1] 19 20 def get_info(self): 21 return ‘stack is :‘,self.st,‘ depth: ‘,len(self.st) 22 if __name__==‘__main__‘: 23 ST = Stack() 24 ST.in_stack(‘a‘) 25 ST.in_stack(‘b‘) 26 ST.in_stack(‘c‘) 27 ST.in_stack(‘d‘) 28 ST.in_stack(‘e‘) 29 print ‘original stack: ‘,ST.get_info() 30 ST.out_stack() 31 ST.out_stack() 32 print ‘stack now is: ‘,ST.get_info()
Stack