Simple cartesian product

P.J.W.S. Vrijlandt P.J.W.S.VRIJLANDT at INT.azg.nl
Thu Jan 6 12:04:29 EST 2000


> Neel Krishnaswami <neelk at brick.cswv.com> wrote:
> 
> I realized this can be condensed even further:
> 
> def product(a, b):
>     return map(lambda n,a=a,b=b: (a[n/len(b)], b[n%len(b)]),
>                range(len(a)*len(b)))
> 
> It's still slower and more memory-hungry than the obvious for-loop,
> though.
> 

Here is my entry: (no len!)

def product(xs, ys):
    return reduce( lambda a, b, xs = xs, ys = ys: a + b
                 , map(lambda x, ys = ys: map(lambda y, x=x: (x, y)
                                              , ys
                                              )
                      , xs
                      )
                 ) 

print product([1, 2, 3], ['a', 'b', 'c'])                 

<layout-doesn't-make-obfuscated-oneliners-readable>-ly yrs

Patrick Vrijlandt










Met vriendelijke groet,

Patrick Vrijlandt




More information about the Python-list mailing list