Simple cartesian product

bryan.olson at uptronics.com bryan.olson at uptronics.com
Wed Jan 5 19:40:35 EST 2000


Magnus L. Hetland wrote:
> "Tim Peters" <tim_one at email.msn.com> writes:
>
> > Search in DejaNews for p13x25.
> >
> > c'mon-folks-new-millennium-new-topics<wink>-ly y'rs  - tim
>
> Now - you're not going to get that "new millennium"-discussion
> started, are you? We are still in the same millennium, century, and
> decade as last year. The third millennium starts with 2001.01.01.

The universe is about 12-billion years old, so
"second millennium" is off by a factor of 6,000,000,
while "third" only by 4,000,000.

Anyway, a thread back in the 19-hundreds was about
the product of any number of lists (well, the number
of lists has to be a non-negative integer).  For
clarity-over-efficiency I like,

def cartesian(*listList):
    if listList:
        result = []
        prod = apply(cartesian, listList[:-1])
        for x in prod:
            for y in listList[-1]:
                result.append(x + (y,))
        return result
    return [()]


--Bryan




Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list