Reputation: 45
I'm using Python's itertools.groupby to count consecutive occurrences of elements as mentioned below
from itertools import groupby
data = [1, 1, 3, 2, 3, 3, 4, 5, 5]
sorted_data = sorted(data)
groups = groupby(sorted_data)
for key, group in groups:
print(key, len(list(group)))
It produces the output as expected
1 2
2 1
3 3
4 1
5 2
But out of curiosity I tried to print the counts alone just to see what happens like below,
from itertools import groupby
data = [1, 1, 3, 2, 3, 3, 4, 5, 5]
sorted_data = sorted(data)
groups = groupby(sorted_data)
for group in groups:
print(len(list(group)))
I get this response
2
2
2
2
2
I've encountered that the groupby function behaves differently when I try to print the counts alone versus when I print both the key and the count.
Why does groupby behave differently in these two cases? an explanation will be great.
Thanks in advance for your help!
Upvotes: -3
Views: 57
Reputation: 2046
your line
print(len(list(group)))
will always give 2. as group is a list of 2 items, where the first item is the index, and the second item is the iterator.
i.e. your group is
(1, <itertools._grouper object at 0x0000029681808220>)
Instead replace it with
print(len(list(list(group)[1])))
Upvotes: 3