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) 
  • For some reason it seems to me that the result of each permutation will be a list ending with fixed elements, before which the first element of the list will go, and all the others in the same order will move one position to the left. Those. example of the original list [2,3,4,5,6,7,8,9] fix [6,7,8,9] result [3,4,5,2,6,7,8,9]. Then the problem is simply solved, without going through the loop. - vostbur
  • numbers are fixed from the end to the second element. numbers will always be natural, starting with a unit and up to n. They are fixed in turn, i.e. first n, then the list is shifted to the right, put n-1, then n-2, etc. then fixed n, n-1 and so on - roG_k70
  • The task is not quite clear, if I am mistaken, correct it. The task to get a list of permutations (a list of lists, as in the loop from the example) or just the result of a sequence of permutations of all elements (modified initial list)? The second is done in one action with simple slices. l = [1, 2, 3, 4, 5], where 4 and 5 are fixed -> result = l [1: 3] + l [0: 1] + l [3: 5] i.e. result is (2, 3, 1, 4, 5) from the example. Calculate the position in the slice is easy, knowing the length of the list and the number of recorded values. - vostbur 6:49 pm

0