A permutation on permutations

Arthur Siegel ajs at ix.netcom.com
Sun Nov 25 18:06:28 CET 2001


Simon writes - 

>well, i'd like to see this list comprehension
>implemented properly ; it doesn't
>compute as it stands (on 2.1.1)...?

Lots of typos in my previous perms function. This
does work - as per Rainer Deyke's post. 

def perms(L):
   if L == [] :
      return [[]]
   return [[L[i]] + p for i in range(len(L)) \
     for p in perms(L[:i] + L[i+1:])]

>To choose, eg. pick only perms
>whos first entry is less than the last

Good sugestion.

But now it gets real strange - at least
to me.

The following little func does not work as I 
would expect!!!

Something about the iteration of p in t 
with the list.remove().

Is the func as written certifiably bad 
Python ,  or a bug/trap I fell into?

M=[1,2,3,4]

def removedups(t):
   for p in t:
      if p[-1]>p[0]:
         t.remove(p)
   print t   

removedups(perms(M))
 
Note that p in t does not iterate over all
items of the original list, so sublists where
p[-1] > p[0] survive!!!

Art







More information about the Python-list mailing list