Author serhiy.storchaka
Recipients Matt Gilson, mgilson, rhettinger, serhiy.storchaka
Date 2017-05-12.15:44:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1494603857.02.0.54060237817.issue30346@psf.upfronthosting.co.za>
In-reply-to
Content
Here is a Python implementation of the idea #2:

from itertools import tee
def groupby(iterable, key=None):
    if key is None:
        key = lambda x: x
    def grouper(currvalue, it, tgtkey):
        yield currvalue
        for currvalue in it:
            if key(currvalue) != tgtkey:
                break
            yield currvalue
    it = iter(iterable)
    tgtkey = init = object()
    while True:
        try:
            currvalue = next(it)
        except StopIteration:
            return
        currkey = key(currvalue)
        if tgtkey is init or currkey != tgtkey:
            tgtkey = currkey
            it, it2 = tee(it)
            yield (currkey, grouper(currvalue, it2, tgtkey))
History
Date User Action Args
2017-05-12 15:44:17serhiy.storchakasetrecipients: + serhiy.storchaka, rhettinger, mgilson, Matt Gilson
2017-05-12 15:44:17serhiy.storchakasetmessageid: <1494603857.02.0.54060237817.issue30346@psf.upfronthosting.co.za>
2017-05-12 15:44:17serhiy.storchakalinkissue30346 messages
2017-05-12 15:44:16serhiy.storchakacreate