Python で List分割してみたのでメモ

#python2.7
def split(list, size):
    def xxx(size):
        return lambda list: lambda index: list[index:index+size]
    return map( xxx(size)(list),  range(0, len(list), size))
>>> split(range(10), 3)
[0, 1, 2]
[3, 4, 5]
[6, 7, 8]
[9]
>>> 


余りを最後の要素に押し込む

def createRange(size, group):
    ret = split(range(size), size/group)
    if size%group >= 1:
        ret[-2] = ret[-2]+ret[-1]
        return ret[:-1]
    else:
        return ret
>>> createRange(10, 3)
[[0, 1, 2], [3, 4, 5], [6, 7, 8, 9]]
>>> createRange(10, 4)

[[0, 1], [2, 3], [4, 5], [6, 7, 8, 9]]