input record seperator (equivalent of "$|" of perl)
Scott David Daniels
Scott.Daniels at Acm.Org
Mon Dec 20 11:36:54 EST 2004
Gábor Farkas wrote:
> Scott David Daniels wrote:
>> If you have python 2.3 or 2.4, you have itertools.
> for me it seems that 2.3 does not have itertools.groupby.
> it has itertools, but not itertools.groupby.
True. The 2.4 document says that itertools.groupby() is equivalent to:
class groupby(object):
def __init__(self, iterable, key=None):
if key is None:
key = lambda x: x
self.keyfunc = key
self.it = iter(iterable)
self.tgtkey = self.currkey = self.currvalue = xrange(0)
def __iter__(self):
return self
def next(self):
while self.currkey == self.tgtkey:
self.currvalue = self.it.next() # Exit on StopIteration
self.currkey = self.keyfunc(self.currvalue)
self.tgtkey = self.currkey
return (self.currkey, self._grouper(self.tgtkey))
def _grouper(self, tgtkey):
while self.currkey == tgtkey:
yield self.currvalue
self.currvalue = self.it.next() # Exit on StopIteration
self.currkey = self.keyfunc(self.currvalue)
So you could always just use that code.
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list