On Fri, 19 Jan 2007 05:04:01 -0800, bearophileHUGS wrote: > Steven D'Aprano: >> > s = "aaabbbbbaabbbbbb" >> > 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 the iterator I need to compute the len of.
Oops, yes you're right. But 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. >> I hope you know what sequences and strings are :-) > > Well, I know little still about the C implementation of CPython > iterators :-) > > But I agree with the successive things you say, iterators may be very > general things, and there are too many drawbacks/dangers, so it's > better to keep leniter() as a function separated from len(), with > specialized use. I don't think it's better to have leniter() at all. If you, the iterator creator, know enough about the iterator to be sure it has a predictable length, you know how to calculate it. Otherwise, iterators in general don't have a predictable length even in principle. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list