Re: Iterator length

2007-01-19 Thread bearophileHUGS
Steven D'Aprano: > since g is not an arbitrary iterator, one can easily do this: > print [(h,len(list(g))) for h,g in groupby(s)] > No need for a special function. If you look at my first post you can see that I have shown that solution too, but it creates a list that may be long, that may use a l

Re: Iterator length

2007-01-19 Thread Steven D'Aprano
On Fri, 19 Jan 2007 05:04:01 -0800, bearophileHUGS wrote: > Steven D'Aprano: >> > s = "aaabaabb" >> > from itertools import groupby >> > print [(h,leniter(g)) for h,g in groupby(s)] >> >> s isn't an iterator. It's a sequence, a string, and an iterable, but not >> an iterator. > > If you l

Re: Iterator length

2007-01-19 Thread bearophileHUGS
Steven D'Aprano: > > s = "aaabaabb" > > from itertools import groupby > > print [(h,leniter(g)) for h,g in groupby(s)] > > s isn't an iterator. It's a sequence, a string, and an iterable, but not > an iterator. If you look better you can see that I use the leniter() on g, not on s. g is th

Re: Iterator length

2007-01-19 Thread Steven D'Aprano
On Thu, 18 Jan 2007 16:55:39 -0800, bearophileHUGS wrote: > What's your point? Maybe you mean that it consumes the given iterator? > I am aware of that, it's written in the function docstring too. But > sometimes you don't need the elements of a given iterator, you just > need to know how many ele

Re: Iterator length

2007-01-18 Thread Gabriel Genellina
At Thursday 18/1/2007 20:26, [EMAIL PROTECTED] wrote: def leniter(iterator): """leniter(iterator): return the length of an iterator, consuming it.""" if hasattr(iterator, "__len__"): return len(iterator) nelements = 0 for _ in iterator: nelements += 1 retu

Re: Iterator length

2007-01-18 Thread Ben Finney
[EMAIL PROTECTED] writes: > But sometimes you don't need the elements of a given iterator, you > just need to know how many elements it has. AFAIK, the iterator protocol doesn't allow for that. Bear in mind, too, that there's no way to tell from outside that an iterater even has a finite length;

Re: Iterator length

2007-01-18 Thread bearophileHUGS
George Sakkis: > Is this a rhetorical question ? If not, try this: It wasn't a rhetorical question. > >>> x = (i for i in xrange(100) if i&1) > >>> if leniter(x): print x.next() What's your point? Maybe you mean that it consumes the given iterator? I am aware of that, it's written in the functi

Re: Iterator length

2007-01-18 Thread George Sakkis
[EMAIL PROTECTED] wrote: > Often I need to tell the len of an iterator, this is a stupid example: > > >>> l = (i for i in xrange(100) if i&1) > > len isn't able to tell it: > > >>> len(l) > Traceback (most recent call last): > File "", line 1, in > TypeError: object of type 'generator' has no l

Iterator length

2007-01-18 Thread bearophileHUGS
Often I need to tell the len of an iterator, this is a stupid example: >>> l = (i for i in xrange(100) if i&1) len isn't able to tell it: >>> len(l) Traceback (most recent call last): File "", line 1, in TypeError: object of type 'generator' has no len() This is a bad solution, it may need t