A permutation on permutations

phil hunt philh at comuno.freeserve.co.uk
Mon Nov 26 01:06:34 CET 2001

On Sun, 25 Nov 2001 15:09:38 -0500, Arthur Siegel <ajs at ix.netcom.com> wrote:
>Phil writes -
>>Can you have the same number appearing twice, e.g. [1,2,3,3] ?
>Actually I hadn't considered double points for what I'm doing. 
>But I believe that the universe of unique curves would 
>include curves with double point args. I'll play with it and see
>if I can get a better criteria for unique curves. 
>>>So am a looking for a function to return all
>>>permutations, excluding duplicates as defined above.
>>Consider the following source:
>>def perm(source, done=0, current=[]):
>>   if done == len(source): 
>>      if current[0] < current[-1]: #removes reversals
>>         print current
>>   else:
>>      for i in source:
>>         if i not in current:
>>            perm(source, done+1, current+[i])
>Works nice, but I haven't found a great way to get a return
>value.  Using a global L=[] in the calling code and 
>an L.append(current) at "print current" in the perm func.

That should do the job; or you could pass the list of all permutations
as a second parameter.

BTW, if you change the 1st if statment to

   if len(current) == len(source):

you can get rid of the (done) variable.

*** Philip Hunt *** philh at comuno.freeserve.co.uk ***

More information about the Python-list mailing list