Re: Into itertools

2009-04-27 Thread bearophileHUGS
Arnaud Delobelle: > Some people would write it as: > > def leniter(iterable): >     if hasattr(iterable, '__len__'): >         return len(iteratble) >     return sum(1 for _ in iterable) That's slower than my version. > > def xpairwise(iterable): > >     return izip(iterable, islice(iterable,

Re: Into itertools

2009-04-27 Thread Mark Dickinson
On Apr 26, 5:32 pm, bearophileh...@lycos.com wrote: > 3) xpairs(seq) >     >>> list(xpairs(range(5))) >     [(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), > (2, 4), (3, 4)] Doesn't itertools.combinations already do this for you? >>> list(itertools.combinations(range(5), 2)) [(

Re: Into itertools

2009-04-27 Thread Arnaud Delobelle
On 26 Apr, 17:32, bearophileh...@lycos.com wrote: > Some idioms are so common that I think they deserve to be written in C > into the itertools module. > > 1) leniter(iterator) [...] > A Python implementation: > > def leniter(iterator): >     if hasattr(iterator, "__len__"): >         return len(it

Re: Into itertools

2009-04-26 Thread Arnaud Delobelle
AggieDan04 writes: > Good suggestions. Another useful function I'd like to see in > itertools is the Cartesian product. This can be implemented as: [snip implementation] You mean itertools.product? http://docs.python.org/library/itertools.html#itertools.product -- Arnaud -- http://mail.pyt

Re: Into itertools

2009-04-26 Thread AggieDan04
On Apr 26, 11:32 am, bearophileh...@lycos.com wrote: > Some idioms are so common that I think they deserve to be written in C > into the itertools module. > > 1) leniter(iterator) ... > 2) xpairwise(iterable) ... > 3) xpairs(seq) ... > 4) xsubsets(seq) ... Good suggestions. Another useful functio

Into itertools

2009-04-26 Thread bearophileHUGS
Some idioms are so common that I think they deserve to be written in C into the itertools module. 1) leniter(iterator) It returns the length of a given iterator, consuming it, without creating a list. I have discussed this twice in the past. Like itertools.izip_longest don't use it with infinite