[Python-ideas] itertools.chunks()
Steven D'Aprano
steve at pearwood.info
Sun Apr 7 08:12:49 CEST 2013
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
More information about the Python-ideas
mailing list