7 Apr
2013
7 Apr
'13
6:12 a.m.
On 06/04/13 23:50, Giampaolo RodolĂ wrote:
def chunks(total, step): assert total >= step while total > step: yield step; total -= step; if total: yield total [...] Now I wonder, would it make sense to have something like this into itertools module?
Since it doesn't operate on iterators, I don't think it belongs in itertools. It can also be implemented like this: def chunks(total, step): a, b = divmod(total, step) for i in range(a): yield step if b: yield b which is probably also less likely to go wrong if you pass float arguments. -- Steven