1.对序列进行分组的函数(摘自web.py源码utils.py文件中) 1 def group(seq, size): 2 """ 3 Returns an iterator over a series of lists of length size from iterable. 4 5 >>> list(group([1,2,3,4], 2)) 6 [[1, 2], [3, 4]] 7 >>> list(group([1,2,3,4,5], 2)) 8 [[1, 2], [3, 4], [5]] 9 """ 10 def take(seq, n): 11 for i in xrange(n): 12 yield seq.next() 13 14 if not hasattr(seq, ‘next‘): 15 seq = iter(seq) 16 17 while True: 18 19 inger=take(seq, size) 20 x = list(inger) 21 if x: 22 yield x 23 else: 24 break