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,
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))
[(
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
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
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
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