Simple cartesian product

Magnus L. Hetland mlh at vier.idi.ntnu.no
Tue Jan 4 18:11:25 EST 2000


mlh at vier.idi.ntnu.no (Magnus L. Hetland) writes:

> def product(a,b):
>     a = reduce(lambda l, x: l + [x]*len(b),
>                [[]] + a)
>     b = b * len(a)
>     return map(None, a, b)
> 

Or, rather:

def product(a,b):
    a2 = reduce(lambda l, x: l + [x]*len(b),
               [[]] + a)
    b  = b * len(a)
    return map(None, a2, b)

(This is getting worse by the minute ;)

--

  Magnus
  Lie
  Hetland



More information about the Python-list mailing list