Python « Перестановка элементов в списках(с фиксацией предыдущих элементов) в Python

Задача заключается в том, чтобы переставлять элементы в списке, не переставляя при этом предыдущий елемент. Например, дан список:
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .python.geshi_code {font-family:monospace;} .python.geshi_code .imp {font-weight: bold; color: red;} .python.geshi_code .kw1 {color: #ff7700;font-weight:bold;} .python.geshi_code .kw2 {color: #008000;} .python.geshi_code .kw3 {color: #dc143c;} .python.geshi_code .kw4 {color: #0000cd;} .python.geshi_code .co1 {color: #808080; font-style: italic;} .python.geshi_code .coMULTI {color: #808080; font-style: italic;} .python.geshi_code .es0 {color: #000099; font-weight: bold;} .python.geshi_code .br0 {color: black;} .python.geshi_code .sy0 {color: #66cc66;} .python.geshi_code .st0 {color: #483d8b;} .python.geshi_code .nu0 {color: #ff4500;} .python.geshi_code .me1 {color: black;} .python.geshi_code span.xtra { display:block; }

x = [1, 2, 3, 4, 5]
 

На первом шаге результатом будет следующий список:
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .python.geshi_code {font-family:monospace;} .python.geshi_code .imp {font-weight: bold; color: red;} .python.geshi_code .kw1 {color: #ff7700;font-weight:bold;} .python.geshi_code .kw2 {color: #008000;} .python.geshi_code .kw3 {color: #dc143c;} .python.geshi_code .kw4 {color: #0000cd;} .python.geshi_code .co1 {color: #808080; font-style: italic;} .python.geshi_code .coMULTI {color: #808080; font-style: italic;} .python.geshi_code .es0 {color: #000099; font-weight: bold;} .python.geshi_code .br0 {color: black;} .python.geshi_code .sy0 {color: #66cc66;} .python.geshi_code .st0 {color: #483d8b;} .python.geshi_code .nu0 {color: #ff4500;} .python.geshi_code .me1 {color: black;} .python.geshi_code span.xtra { display:block; }

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
 

На втором шаге мы рассматриваем по уровням. Берем первый элемент списка new_x и делаем перестановку сначала в нем, затем во втором елементе и тд. На примере первого элемента:
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .python.geshi_code {font-family:monospace;} .python.geshi_code .imp {font-weight: bold; color: red;} .python.geshi_code .kw1 {color: #ff7700;font-weight:bold;} .python.geshi_code .kw2 {color: #008000;} .python.geshi_code .kw3 {color: #dc143c;} .python.geshi_code .kw4 {color: #0000cd;} .python.geshi_code .co1 {color: #808080; font-style: italic;} .python.geshi_code .coMULTI {color: #808080; font-style: italic;} .python.geshi_code .es0 {color: #000099; font-weight: bold;} .python.geshi_code .br0 {color: black;} .python.geshi_code .sy0 {color: #66cc66;} .python.geshi_code .st0 {color: #483d8b;} .python.geshi_code .nu0 {color: #ff4500;} .python.geshi_code .me1 {color: black;} .python.geshi_code span.xtra { display:block; }

1, 2, 3, 4, 5
1, 2, 4, 3, 5
1, 3, 4, 2, 5
2, 3, 4, 1, 5
 

и так далее. Причем сделать это нужно так, чтобы эти перестановки удобно было использовать, ведь это часть общей задачи, которая доступна по ссылке ниже (начиная с 7ой страницы):
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .html4strict.geshi_code {font-family:monospace;} .html4strict.geshi_code .imp {font-weight: bold; color: red;} .html4strict.geshi_code .kw2 {color: #000000; font-weight: bold;} .html4strict.geshi_code .kw3 {color: #000066;} .html4strict.geshi_code .es0 {color: #000099; font-weight: bold;} .html4strict.geshi_code .br0 {color: #66cc66;} .html4strict.geshi_code .sy0 {color: #66cc66;} .html4strict.geshi_code .st0 {color: #ff0000;} .html4strict.geshi_code .nu0 {color: #cc66cc;} .html4strict.geshi_code .sc-1 {color: #808080; font-style: italic;} .html4strict.geshi_code .sc0 {color: #00bbdd;} .html4strict.geshi_code .sc1 {color: #ddbb00;} .html4strict.geshi_code .sc2 {color: #009900;} .html4strict.geshi_code span.xtra { display:block; }

https://docs.google.com/open?id=1PwvuPKXPt3caY32c9soHXU1nTpEQaNWUvvTfWwFkDVh9QOQSrM3ZE1jg--s0
 

1 ответов


Помог с ответом человек peter de rivaz на stackoverflow:
Here is some Python code for the updated question:


def perms(A):
    if len(A)==1:
        yield A
    for i in xrange(len(A)-1,-1,-1):
        for B in perms(A[:i]+A[i+1:]):
            yield B+A[i:i+1]
 
Running


for a in perms([1,2,3,4]):
    print a
prints the following:
 


[1, 2, 3, 4]
[2, 1, 3, 4]
[1, 3, 2, 4]
[3, 1, 2, 4]
[2, 3, 1, 4]
[3, 2, 1, 4]
[1, 2, 4, 3]
[2, 1, 4, 3]
[1, 4, 2, 3]
[4, 1, 2, 3]
[2, 4, 1, 3]
[4, 2, 1, 3]
[1, 3, 4, 2]
[3, 1, 4, 2]
[1, 4, 3, 2]
[4, 1, 3, 2]
[3, 4, 1, 2]
[4, 3, 1, 2]
[2, 3, 4, 1]
[3, 2, 4, 1]
[2, 4, 3, 1]
[4, 2, 3, 1]
[3, 4, 2, 1]
[4, 3, 2, 1]