Into itertools
AggieDan04
danb_83 at yahoo.com
Sun Apr 26 13:19:11 EDT 2009
On Apr 26, 11:32 am, bearophileH... at 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 function I'd like to see in
itertools is the Cartesian product. This can be implemented as:
def cartesian_product(*args):
"""Iterates over the Cartesian product of args[0], args[1], ..."""
if not args:
return
elif len(args) == 1:
for item in args[0]:
yield (item,)
else:
for item in args[0]:
for item2 in cartesian_product(*args[1:]):
yield (item,) + item2
More information about the Python-list
mailing list