manipulating the output of itertools.permutations in python -


i want take list, instance list = [1,2,2], , generate permutations. can with:

newlist = [list(itertools.permutations(list))] 

and output is:

[[(1, 2, 2), (1, 2, 2), (2, 1, 2), (2, 2, 1), (2, 1, 2), (2, 2, 1)]] 

the problem: itertools.permutations returns list of length 1 entry list of permutations of list. is:

newlist[0] == [(1,2,2),(1,2,2),(2,1,2),(2,2,1),(2,1,2),(2,2,1)] 

and

newlist[1] not exist.

i want output list each entry 1 of permutations. is

newlist[0] == (1,2,2) newlist[1] == (1,2,2) newlist[2] == (2,1,2) ... newlist[5] == (2,2,1) 

the question: there command produce permutations of list in way? failing that, there way access 'individual elements' of [list(itertools.permutations(list))] can things them?

>>> itertools import permutations >>> list(permutations([1,2,2])) [(1, 2, 2), (1, 2, 2), (2, 1, 2), (2, 2, 1), (2, 1, 2), (2, 2, 1)] 

you don't need put in list again. i.e don't [list(permutations(...))] (by doing [] making nested list , hence unable access elements using testlist[index], though testlist[0][index] better keep list of tuples.)

>>> newlist = list(permutations([1, 2, 2])) >>> newlist[0] (1, 2, 2) >>> newlist[3] (2, 2, 1) 

now can access elements using indices.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -