python官方教程,關於list的練習python
print 'this is about list test' a =[1,2,3,4] a.append(5) print a b =[5,6] a.extend(b) print a a.insert(0,0) print a a.insert(len(a),10) print a a.pop() print a a.reverse() print a print 'use list as stack' stack = [1,2] stack.append(3) stack.append(4) print stack stack.pop() stack.pop() print stack print 'use list as sequence' from collections import deque queue = deque(['aa','bb','cc']) queue.append('dd') queue.append('ee') print queue.popleft() print queue print 'functional programming tools three important function use' print 'filter use' def f(x): return x%3==0 or x%5==0 ll = filter(f,range(2,25)) print ll print 'map use' def cube(x): return x*x*x cc = map(cube,range(1,10)) print cc print 'reduce use ' def add(a,b): return a+b i = reduce(add,range(1,11)) print i print 'list comprehension' squares = [x**2 for x in range(10)] print squares mm = [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] print mm print 'nested list comprehension' matrix = [ [1,2,3,4], [5,6,7,8], [9,10,11,12] ] print matrix print 'The following list comprehension will transpose rows and columns:' new_matrix = [[row[i] for row in matrix] for i in range(4)] print new_matrix