all possible combinations

rbt rbt at athop1.ath.vt.edu
Wed Jul 13 10:39:41 EDT 2005


On Thu, 2005-07-14 at 00:47 +1000, Steven D'Aprano wrote:
> 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?

The CPU

> 
> 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).

My list is not arbitrary. I'm looking for all 'combinations' as I
originally posted. Order does not matter to me... just all
possibilities.




More information about the Python-list mailing list