python - Control the iteration range of the generator? -
is there way can control iteration range of python generator object?
example: generator consists of 2000 items, want 1 loop iterate 1 100, , 101 200, on.
a simple for item in generator
loop deplete generator
itertools.islice(generator, start, stop, step)
want use.
for example:
def generator(): n = 1 while n: yield n n += 1 items = generator() item in itertools.islice(items, 100): print ('first', item) item in itertools.islice(items, 100): print ('second', item)
edit: above of course lets take 1 hundred each time. if know generator finite make list , iterate on ranges of it. have arbitrary , overlapping ranges.
Comments
Post a Comment