[New-bugs-announce] [issue31614] can't list groupby generator without breaking the sub groups generators
Loïc Le Loarer
report at bugs.python.org
Wed Sep 27 16:55:49 EDT 2017
New submission from Loïc Le Loarer <loic.le-loarer at m4x.org>:
If I "list" the itertools groupby generator, then the sub generators of each groups are all empty except the last one.
import itertools as i
L = ['azerty','abcd','ac','aaa','z','baba','bitte','rhum','z','y']
g = list(i.groupby(L, lambda x: x))
number_of_groups = len(g)
ans = 0
for k, v in g: # This doesn't work
#for k, v in i.groupby(L, lambda x: x): # This works
v = list(v)
ans += 100*len(v)//number_of_groups
assert(ans == 163)
I don't understand why. Is my code broken ?
The need for saving the group generator first exists when I need the number of groups before walking thru the groups, like in the above example.
I have not been able to test to latest python versions, is the problem already fixed ?
nosy: Loïc Le Loarer
title: can't list groupby generator without breaking the sub groups generators
versions: Python 2.7, Python 3.5
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce