- list: append,inset,remove,pop,index,count,sort,reverse,extend
>>> list=[1,2]
>>> list.append(3)
>>> list
[1, 2, 3]
>>> list.insert(0,4)
>>> list
[4, 1, 2, 3]
>>> list.remove(1)
>>> list
[4, 2, 3]
>>> list.pop()
3
>>> list
[4, 2]
>>> list.index(4)
0
>>> list.count(4)
1
>>> list.sort()
>>> list
[2, 4]
>>> list.reverse()
>>> list
[4, 2]
>>> list.extend(list)
>>> list
[4, 2, 4, 2]
- list as stack
>>> stack = [1,2,3]
>>> stack.append(4)
>>> stack.pop()
4
- list as queue
caution! : "module: from collections import deque " and "constuction deque([1,2,3]) "
>>> from collections import deque
>>> queue = deque([1,2,3])
>>> queue.append(4)
>>> queue.popleft()
- filter(),map(),and reduce():built-in function usefull for list
filter():
>>> def odd(num):
... return num%2==1
...
>>> filter(odd,range(0,5))
[1, 3]
- map():
>>> def square(num):
... return num*num
...
>>> map(square,range(0,3))
[0, 1, 4]
- reduce():
>>> def add(x,y): return x+y
...
>>> reduce(add, range(1, 11))
55
- matrix: mutidimension array(list)
>>> matrix = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]]
>>> matrix[0][0]
1
>>> zip(*matrix)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
- del(delete)
>>> list = [1, 2, 3]
>>> del list[0]
>>> list
[2, 3]
- tuples: separated by commas
>>> t = 12345, 54321, 'hello!'
>>> t[0]
12345
>>> t
(12345, 54321, 'hello!')
>>> # Tuples may be nested:
... u = t, (1, 2, 3, 4, 5)
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
- set
>>> fruit = set(['apple','orange'])
>>> 'apple' in fruit
True
- Dictionaries
>>> tel = {'huizhou':'0752', 'guangzhou':'020'}
>>> tel['guangzhou']
'020'
>>> 'huizhou' in tel
True
-
Looping Techniques
>>> for name,num in tel.iteritems():
... print name, num
...
guangzhou 020
huizhou 0752