all possible combinations

Thomas Bartkus thomasbartkus at comcast.net
Wed Jul 13 21:20:20 CEST 2005


"George Sakkis" <gsakkis at rutgers.edu> wrote in message
news:1121277937.a2a3097d7c150f1b8a3f41a21a9f2b25 at teranews...
> "rbt" <rbt at athop1.ath.vt.edu> wrote:
>
> > Say I have a list that has 3 letters in it:
> >
> > ['a', 'b', 'c']
> >
> > I want to print all the possible 4 digit combinations of those 3
> > letters:
> >
> > 4^3 = 64
>
>
> It's actually 3^4 = 81 (3 candidates/choice ** 4 choices)

Yes.  You get a cigar!

Someone else (Jack Diederich) also mentioned "This is called a cartesian
product, ..."
Right again.

Now I can't help injecting that SQL is custom made for returning a cartesian
product.

Given a table called [Letters] containing a field [Letter] with (3) records
Letter = 'a', 'b', 'c'

SELECT CONCAT(t1.Letter, t2.Letter, t3.Letter, t4.Letter)
FROM Letters As t1, Letters As t2, Letters As t3, Letters As t4

Will return those 81 combinations in an eyblink.

In order to stay on topic, you are required to call your favorite SQL engine
from Python :-)
Thomas Bartkus





More information about the Python-list mailing list