The maximum number of element in a list when generate all the permutations of a list in Python -


i using itertools generate permutations of python list

import itertools lt = [0,1,2,3] print list(set(itertools.permutations(lt))) 

it works when length of list less 7, when length of list grater 7 ([0,1,2,3,4,5,6,7]), takes lot of time generate permutations, , program frozen. wondering if there way generate permutations big list, thank you!

itertools return generator in ns time. it's slow conversion set().

8!

>>> lt = [i in xrange(8)] >>> %timeit itertools.permutations(lt) 1000000 loops, best of 3: 547 ns per loop  >>> %timeit set(itertools.permutations(lt)) 100 loops, best of 3: 13.4 ms per loop  >>> %timeit list(set(itertools.permutations(lt))) 10 loops, best of 3: 19.1 ms per loop  >>> %timeit list(itertools.permutations(lt)) 100 loops, best of 3: 4.9 ms per loop 

9! = 362880

>>> lt = [i in xrange(9)]  >>> %timeit itertools.permutations(lt) 1000000 loops, best of 3: 557 ns per loop  >>> %timeit set(itertools.permutations(lt)) 1 loops, best of 3: 167 ms per loop  >>> %timeit list(set(itertools.permutations(lt))) 1 loops, best of 3: 186 ms per loop  >>> %timeit list(itertools.permutations(lt)) 10 loops, best of 3: 61.9 ms per loop 

compared forloop generate list length(362880)

>>> %timeit list(i in xrange(362880)) 10 loops, best of 3: 33 ms per loop 

i think performance considerably enough. btw, i'm using python2.7


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -