Reputation: 2784
def list_step(max = 268):
""" split in steps of 120 elements at time
"""
if max > 120:
for i in xrange(0,max,120):
if i <= max:
rest = max % xrange(0,max,120)[-1]
if rest != 120:
yield(i, rest)
else:
yield(i, 0)
else:
yield(max)
a = list_step()
a.next() return > (0,28) (120,28),ecc
Would it be possible to return the rest on the execution of last next(), instead of the tuple with the rest?
so that :
a.next() return > (0) (120),ecc.. (28)
Upvotes: 0
Views: 682
Reputation: 7384
You can use itertools.chain
to chain iterators together (documentation). If you simply want a single value "appended" to your generator you can use it (note that you need to somehow turn a single item into an iterable).
Also your max % xrange(0, max, 120)[-1]
will always be max % 120
, because xrange(0, max, 120)
is the biggest value that is a multiple of 120 that is smaller as max, so dividing by it will yield the same result as dividing by 120 (modulo).
import itertools
itertools.chain(xrange(0,max,120), [max % 120])
Upvotes: 1
Reputation: 1722
something like this ?
def list_step(max = 268):
""" split in steps of 120 elements at time
"""
if max > 120:
rest_list = [];
for i in xrange(0,max,120):
if i <= max:
rest = max % xrange(0,max,120)[-1]
if rest != 120:
yield(i)
rest_list.append(rest)
else:
yield(i)
rest_list.append(0)
for i in rest_list:
yield(i)
else:
yield(max)
a = list_step()
for i in a: print i;
Upvotes: 0