標籤:
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...‘% x12 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
Python基礎之:List