combinatorics via __future__ generators
John Yeung
gallium.arsenide at gmail.com
Wed Dec 2 00:35:12 EST 2009
On Dec 1, 5:55 pm, Phlip <phlip2... at gmail.com> wrote:
> Awesome thanks - but:
>
> > from itertools import imap,product
>
> Do we have a version for Python2.5? I have to support an older server
> here; can't install a newer python on it...
If you can get by with the performance of pure Python, a solution is
right in the documentation for 2.6:
###
def product(*args, **kwds):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = map(tuple, args) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
###
The docs for itertools are full of these definitions (for 2.6
functions) that can be used as recipes (if you don't have 2.6).
John
More information about the Python-list
mailing list