all possible combinations

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Jul 13 10:47:33 EDT 2005


On Wed, 13 Jul 2005 10:21:19 -0400, rbt 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?

Efficient for who? The user? The programmer? The computer? Efficient use
of speed or memory or development time?

If you want the fastest runtime efficiency, a lookup table of
pre-calculated values. That is an O(1) operation, and you don't get any
faster than that.

If you expect to extend the program to arbitrary lists, pre-calculation
isn't practical, so you need an algorithm to calculate permutations (order
matters) or combinations (order doesn't matter).

If you tell us what you need, I'm sure somebody will be able to help meet
those needs. Otherwise, we're just guessing what you want.



-- 
Steven.




More information about the Python-list mailing list