2

Right now I have this python function:

def groupBy(iterable, N):
    ret = []
    count = 0
    for item in iterable:
        if count < N:
            count = count + 1
        else:
            yield ret
            count = 1
            ret = []
        ret.append(item)
    yield ret

>>> [iter for iter in groupBy(xrange(25), 10)]

[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24]]

The problems are:

  • I have no way to know iterable and N before hand
  • Futher processing on each item on iter is needed

I would like to be memory efficient. So instead of yield a list in groupBy function, is it possible to yield another generator?

For the same input [iter for iter in groupBy(xrange(25), 10)], return something like

[<generator object subGenerator at 0x000001000>,
 <generator object subGenerator at 0x000002000>,
 <generator object subGenerator at 0x000003000>]
user2829759
  • 3,372
  • 2
  • 29
  • 53
  • `I would like to be memory efficient` always a good goal, but remember that premature optimization is the root of all evil. Try your algorithm out in expected, worst case, and engineered attacks to see if it fails. – Wayne Werner Jan 21 '16 at 20:27

0 Answers0