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
andN
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>]