I ran into this problem:
It is necessary to make a series of permutations, at each step fixing the previous element.
For example, if we have a list of 5 elements (1,2,3,4,5), then in the first step the result will be as follows:
(1, 2, 3, 4, 5) (1, 2, 3, 5, 4) (1, 2, 4, 5, 3) (1, 3, 4, 5, 2) (2, 3, 4, 5, 1)
We received 5 lists, now on each of them we fix the last element, i.e. in the first one we fix 5, in the second 4, ..., in the last 1 and rearrange them to n-1 - th place (ie, 4th) numbers in turn from left to right, i.e. for the list (1, 2, 3, 4, 5 ), 5 is no longer participating in the permutations, but all the numbers to the left of it, ie:
(1, 2, 3, 4, 5) (1, 2, 4, 3, 5) (1, 3, 4, 2, 5) (2, 3, 4, 1, 5)
Then go to the list (1, 2, 3, 4 , 5 ), where 4 and 5 are fixed, and we get:
(1, 2, 3, 4, 5) (1, 3, 2, 4, 5) (2, 3, 1, 4, 5)
Well, and so on. In more detail you can look in the document on Google Docs (from the 7th page).
For example, I wrote code that rearranges only the last element:
exes = [1, 2, 3, 4, 5] vertices0, templist = [], [] for i in xrange(len(exes)): templist = exes[:] tmp = templist[i] del templist[i] templist.append(tmp) vertices0.append(templist)