On Sun, 23 Dec 2007 02:22:38 -0800, cf29 wrote: > How would you write a function that will populate a list with a list of > numbers with all the possibilities? For example a list of 3 numbers > taken among 4 [0,1,2,3] without duplicates. The result should be: > [0,1,2] > [0,1,3] > [0,2,3] > [1,2,3]
What you are asking for is the combinations of the list, taking 3 elements at a time. Try using this generator: def combinations(seq, n): if n == 0: yield [] else: for i in xrange(len(seq)): for cc in combinations(seq[i+1:], n-1): yield [seq[i]]+cc >>> for c in combinations(range(4), 3): ... print c ... [0, 1, 2] [0, 1, 3] [0, 2, 3] [1, 2, 3] -- Steven -- http://mail.python.org/mailman/listinfo/python-list