Bryan wrote:

> can some explain why in the 2nd example, m doesn't print the list [1, 1, 1]
> which i had expected?
>
>
>  >>> for k, g in groupby([1, 1, 1, 2, 2, 3]):
> ...     print k, list(g)
> ...
> 1 [1, 1, 1]
> 2 [2, 2]
> 3 [3]
>
>
>  >>> m = list(groupby([1, 1, 1, 2, 2, 3]))
>  >>> m
> [(1, <itertools._grouper object at 0x00AAC600>), (2, <itertools._grouper 
> object
> at 0x00AAC5A0>), (3, <itertools._grouper object at 0x00AAC5B0>)]
>  >>> list(m[0][1])
> []
>  >>>
>
>
> thanks,
>
> bryan

I've tripped on this more than once, but it's in the docs
(http://docs.python.org/lib/itertools-functions.html):

"The returned group is itself an iterator that shares the underlying
iterable with groupby(). Because the source is shared, when the groupby
object is advanced, the previous group is no longer visible. So, if
that data is needed later, it should be stored as a list"

George

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to