[python]将列表分组成包含多个子列表的列表
想取得的效果是: [1, 2, 3, 4, 5, 6, 7] --> [[1, 2, 3], [4, 5, 6], [7]]
下面三种方法取自博文 http://sandrotosi.blogspot.com/2011/04/python-group-list-in-sub-lists-of-n.html 及评论。
第一种方法
def group_iter(iterator, n=2): """ Given an iterator, it returns sub-lists made of n items (except the last that can have len < n) inspired ...