0
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)
user2239318
  • 2,578
  • 7
  • 28
  • 50
  • 2
    It's not clear what you mean - could you give a few short, representative examples of how this would look in an interpreter session? – jonrsharpe Mar 20 '15 at 10:28
  • Can you give context on why/how you would use this? It seems to me that `zip(xrange(0,max,120), [max % 120]*max)` would do the same. You can cain iterators with `itertools.chain`. – syntonym Mar 20 '15 at 10:29

2 Answers2

1

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])
syntonym
  • 7,134
  • 2
  • 32
  • 45
  • 1
    This answer would be more useful if you explained how the code works. – skrrgwasme Mar 20 '15 at 11:29
  • @skrrgwasme What exactly is unclear? `xrange` is an generator, `_yield` is an generator, `itertools.chain` chains to iterables together. I'm not sure if it really does what the questioner wants, because the accepted answer produces something differently, but I don't think the question defines it precisely enough to be sure. – syntonym Mar 20 '15 at 13:46
  • 1
    Think about your intended audience. The OP (and future visitors) are here because they don't understand something. We could give everyone code snippets that they can copy and paste, but that doesn't really aid in understanding. There's more lasting value to the community in answers that explain how things work. – skrrgwasme Mar 20 '15 at 14:24
0

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;
SolaWing
  • 1,652
  • 12
  • 14