1 1.对序列进行分组的函数(摘自web.py源码utils.py文件中) 2 def group(seq, size): 3 """ 4 Returns an iterator over a series of lists of length size from iterable. 5 6 >>> list(group([1,2,3,4], 2)) 7 [[1, 2], [3, 4]] 8 >>> list(group([1,2,3,4,5], 2)) 9 [[1, 2], [3, 4], [5]] 10 """ 11 def take(seq, n): 12 for i in xrange(n): 13 yield seq.next() #当seq迭代到最后一个元素直到没有时,抛出StopIteration被外层的list()捕获 14 if not hasattr(seq, 'next'): 15 seq = iter(seq) 16 while True: 17 inger=take(seq, size) 18 x = list(inger) 19 if x: 20 yield x 21 else: 22 break