简单总结以及整理如下:
>>> 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]
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'])
Q:
1、取两个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]
参考资料:
Expert Python Programming
http://docs.python.org/2/tutorial/datastructures.html#more-on-lists