[Numpy-discussion] Permutation in Numpy

Hee-Seng Kye kyeser at earthlink.net
Sun Jul 25 04:25:14 CDT 2004


#perm.py
def perm(k):
     # Compute the list of all permutations of k
     if len(k) <= 1:
         return [k]
     r = []
     for i in range(len(k)):
         s =  k[:i] + k[i+1:]
         p = perm(s)
         for x in p:
             r.append(k[i:i+1] + x)
     return r

Does anyone know if there is a built-in function in Numpy (or Numarray) 
that does the above task faster (computes the list of all permutations 
of a list, k)?  Or is there a way to make the above function run faster 
using Numpy?

I'm asking because I need to create a very large list which contains 
all permutations of range(12), in which case there would be 12! 
permutations.  I created a file test.py:

#!/usr/bin/env python
from perm import perm
print perm(range(12))

And ran the program:

$ ./test.py >> list.txt

The program ran for about 90 minutes and was still running on my 
machine (667 MHz PowerPC G4, 512 MB SDRAM) until I quit the process as 
I was getting nervous (and impatient).

I would highly appreciate anyone's suggestions.

Many thanks,
Kye





More information about the Numpy-discussion mailing list