all possible combinations

William Park opengeometry at yahoo.ca
Thu Jul 14 17:10:37 EDT 2005


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
> 
> aaaa
> abaa
> aaba
> aaab
> acaa
> aaca
> aaac
> ...
> 
> What is the most efficient way to do this? 

Since you're doing cross product (ie. 3*3*3*3), manual loop of 4 level
deep would be the fastest in terms of algorithm.  C vs. Python is
implementation detail.

In Bash shell, this is one-liner,
    echo {a,b,c}{a,b,c}{a,b,c}{a,b,c}
or maybe two,
    abc=(a b c)
    echo {^abc}{^abc}{^abc}{^abc}

-- 
William Park <opengeometry at yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
	   http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
	  http://freshmeat.net/projects/bashdiff/



More information about the Python-list mailing list