现在我有这个python函数:
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]]
问题是:
iterable
和N
iter
上的每个项目进行进一步处理我希望能够提高内存效率。
因此,不是在groupBy
函数中产生一个列表,是否可以产生另一个生成器?
对于相同的输入[iter for iter in groupBy(xrange(25), 10)]
,返回类似
[<generator object subGenerator at 0x000001000>,
<generator object subGenerator at 0x000002000>,
<generator object subGenerator at 0x000003000>]