Combinations of n Lists

Tim Hochberg tim.hochberg at ieee.org
Thu Mar 1 12:32:49 EST 2001


"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:97lqcm030au at news2.newsguy.com...
> "Tim Hochberg" <tim.hochberg at ieee.org> wrote in message
> news:89bn6.1121$DY.186465 at news1.rdc1.az.home.com...
>     [snip]
> > > def combinations(list1,list2):
> > >     return [ (i,j) for i in list1 for j in list2 ]
> > >
> > > print combinations( [1,2,3], ['a','b','c'] )
> > >
> > > [(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'),
> (3,
>     [snip]
> > zip([1,2,3], [4,5,6], [7,8,9], .... )
>
> Nope -- this only gives [(1,4,7),(2,5,8),(3,6,9)], *not* all of the
> other 24 requested combinations (1,4,8), (1,4,9), ..., (3,6,8).

Foiled again. I didn't read the original posting very closely I guess.
Sigh....

-tim





More information about the Python-list mailing list