This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author greg.solomon
Recipients docs@python, greg.solomon
Date 2016-12-11.20:16:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1481487383.57.0.242190046836.issue28939@psf.upfronthosting.co.za>
In-reply-to
Content
https://docs.python.org/3/library/itertools.html#itertools.groupby

I found the "equivalent" code a bit hard to read. Is there any merit in changing it to something a bit like the following ?

Kind Rgds, Greg

class groupby:
    def __init__( self , iterable , key_function = ( lambda x: x ) ):
        self.iterable = iter( iterable )
        self.key_function = key_function
        self.FINISHED = object()
        try:
            self.next_value = next( self.iterable )
        except StopIteration: 
            self.next_value = self.FINISHED
    def __iter__( self ):
        return self
    def __next__( self ):
        if self.next_value == self.FINISHED:
            raise StopIteration
        self.group_key_value = self.key_function( self.next_value )
        return ( self.group_key_value , self._group() )
    def _group( self ):
        while self.next_value != self.FINISHED \
          and self.group_key_value == self.key_function( self.next_value ):
            yield self.next_value
            try:
                self.next_value = next( self.iterable )
            except StopIteration:
                self.next_value = self.FINISHED 
        return
History
Date User Action Args
2016-12-11 20:16:23greg.solomonsetrecipients: + greg.solomon, docs@python
2016-12-11 20:16:23greg.solomonsetmessageid: <1481487383.57.0.242190046836.issue28939@psf.upfronthosting.co.za>
2016-12-11 20:16:23greg.solomonlinkissue28939 messages
2016-12-11 20:16:23greg.solomoncreate