Phthon list(转)
简单总结以及整理如下:
>>> dir(list) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
引用
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 utterable
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
列表推导(list comprehensions)
>>> [i for i in range(10) if i%2==0] [0, 2, 4, 6, 8] >>> def f(x):return x%2!=0 and x%3!=0 >>> filter(f, range(2,25)) # filter返回最终值为True的value的list [5, 7, 11, 13, 17, 19, 23] >>> seq = range(8) >>> def add(x,y):return x+y >>> map(add, seq, seq) [0, 2, 4, 6, 8, 10, 12, 14] # map 会去遍历后面的值然后依次执行函数 >>> map(lambda x,f = lambda x,f:f(x-1,f)+f(x-2,f) if x >1 else x:f(x,f),range(10)) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] >>> def add(x,y):return x+y >>> reduce(add,range(1,11)) # reduce会将上一次的结果保存下来作为x,并参与与下一个值y的运算中去 55
#用传给reduce中的函数 func()(必须是一个二元操作函数)先对集合中的第1,2个数据进行操作,得到的结果再与第三个数据用func()函数运算,最后得到一个结果 >>> def sum(seq): def add(x,y):return x+y return reduce(add, seq, 0) >>> sum(range(1,11)) 55 >>> sum([]) 0
use enumerate
>>> seq = ["one", "two", "three"] >>> for i, element in enumerate(seq): ... print i, element ... 0 one 1 two 2 three
用Lists作为Stacks
>>> stack = [3,4,5] >>> stack.append(6) >>> stack.append(7) >>> stack [3, 4, 5, 6, 7] >>> stack.pop() 7 >>> stack [3, 4, 5, 6] >>> stack.pop() 6 >>> stack [3, 4, 5] >>> stack.pop() 5 >>> stack [3, 4]
用Lists作为Queues
>>> from collections import deque >>> queue = deque(["Eric", "John", "Michael"]) >>> queue.append("Terry") >>> queue.append("Graham") >>> queue.popleft() 'Eric' >>> queue.popleft() 'John' >>> queue deque(['Michael', 'Terry', 'Graham'])
Functional Programming Tools
>>> def f(x):return x%2!=0 and x%3!=0 ... >>> filter(f, range(2,25)) [5, 7, 11, 13, 17, 19, 23] >>> seq = range(8) >>> def add(x,y):return x+y ... >>> map(add, seq, seq) [0, 2, 4, 6, 8, 10, 12, 14] >>> map(lambda x,f = lambda x,f:f(x-1,f)+f(x-2,f) if x >1 else x:f(x,f),range(10)) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] >>> def add(x,y):return x+y ... >>> reduce(add,range(1,11)) 55 >>> def sum(seq): ... def add(x,y):return x+y ... return reduce(add, seq, 0) ... >>> sum(range(1,11)) 55 >>> sum([]) 0
Nested List Comprehensions
>>> matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] >>> [[row[i] for row in matrix] for i in range(4)] [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
The del statement
>>> a = [1,2,3,4,5,6,7,8,9,0] >>> del a[0] >>> a [2, 3, 4, 5, 6, 7, 8, 9, 0] >>> del a[2:4] >>> a [2, 3, 6, 7, 8, 9, 0] >>> del a[:] >>> a []
Tuples and Sequences
>>> empty = () >>> singleton = 'hello', >>> len(empty) 0 >>> len(singleton) 1 >>> singleton ('hello',) >>> t=1,2,3 >>> t (1, 2, 3) >>> x, y,z = t
Sets
#Similarly to list comprehensions, set comprehensions are also supported: >>> a = {x for x in 'abracadabra' if x not in 'abc'} >>> a set(['r', 'd'])
取两个list的交集和差集
>>> l1=[1,2,3,4,5,6,7,8,9,0] >>> l2=[2,4,6,8] >>> list(set(l1).intersection(l2)) [8, 2, 4, 6] >>> list(set(l1).difference(l2)) [0, 1, 3, 5, 7, 9]
切片操作/索引
>>> l = ['abc', 'def', 'ghi', 123] >>> l[2] 'ghi' >>> l[-3] 'def' >>> l[:3] ['abc', 'def', 'ghi'] >>> l[1] = 'haha' >>> l ['abc', 'haha', 'ghi', 123] >>> l[1:] = ['apple', 'banana'] >>> l ['abc', 'apple', 'banana'] >>> l[2] = [123, 345, 456] >>> l ['abc', 'apple', [123, 345, 456]] >>> l[1:] = [123, 234, 345, 456, 567] >>> l ['abc', 123, 234, 345, 456, 567]
-------------------------------------------
l[a:b] # 从index为a开始取,直到index为b-1
l[::-1] # 相当于l.reverse()之后l的值,也就是说不会产生新的list
参考资料:
Expert Python Programming
http://docs.python.org/2/tutorial/datastructures.html#more-on-lists