简体   繁体   English

按字典顺序生成列表的所有排列

[英]Generate all permutations of a list in lexicagraphical order

I am very much a beginner. 我非常新手。 I am trying to write a program that, given the number of elements (1-9) in the list as a parameter, will then output all permutations of the list in lexicographical order. 我正在尝试编写一个程序,给定列表中元素的数量(1-9)作为参数,然后将其按字典顺序输出列表的所有排列。 In the program it is adding on each permutation as a list into a larger list that contains all the permutations in order. 在程序中,它将每个排列作为一个列表添加到一个更大的列表中,该列表按顺序包含所有排列。 Although the program is not working as expected in general, one main problem I'm having is with this while loop In line 10, I want the list to stop compiling once the final permutation has been added to the list. 尽管程序通常无法按预期方式运行,但是我遇到的一个主要问题是while循环在第10行中,我希望一旦将最终排列添加到列表中,列表就停止编译。 For example, if my input parameter is n = 4, the last permutation/element should be [4,3,2,1]. 例如,如果我的输入参数为n = 4,则最后的排列/元素应为[4,3,2,1]。 However, when I run this program, that element is in the list three times at the end. 但是,当我运行该程序时,该元素在列表末尾出现了3次。 I don't know how this is so when it should terminate that while loop once it has been added. 我不知道这是怎么回事,当它添加完后应该终止while循环。

def ourPermutations(n):
    x=list(range(1,n+1))
    permList = []
    permList+=[x]

    xcopy = x[:]
    finalPerm = xcopy[::-1]


    while x != finalPerm:
        istar = n-2
        while x[istar] > x[istar+1]:
            istar -= 1
        jstar = n-1
        while x[jstar] < x[istar]:
            jstar -= 1
        x[istar],x[jstar] = x[jstar],x[istar]
        if istar+1 == n-1:
            x = x[:]
        else:
            a = x[istar+1:]
            a = a[::-1]
            x = x[:istar+1] + a
        permList += [x]

    return permList

That is my main question; 这是我的主要问题; however, this program is still missing elements when I run it. 但是,当我运行该程序时,它仍然缺少元素。 It isn't quite working, so if you see a spot where something is obviously wrong, feel free to tell me that particular line is what is causing my problems. 它不是很有效,因此,如果您发现某个地方明显存在问题,请随时告诉我特定的行是造成我问题的原因。 If it helps, this is based on this identical (and correct) version written in Mathematica 8: 如果有帮助,这是基于用Mathematica 8编写的相同(正确)版本:

ourpermutations[n_] := (
    ourlist = {x=Range[1,n]};
    While[
        x != Reverse[Range[1,n]],
        istar = n-1;
        While[x[[istar]] > x[[istar+1, istar--];
        jstar = n; While[x[[jstar]] < x[[istar]], jstar--];
        x[[{istar, jstar}]] = x[[{jstar, istar}]];
        AppendTo[ourlist, x = Join[Take[x,istar], Reverse[Drop[x,istar]]]]
    ];
    ourlist
)

So this is what my Python code should be doing; 这就是我的Python代码应该做的; I just can't get it to do so just yet. 我只是无法做到这一点。 Thanks for any of your time and effort. 感谢您的时间和精力。

It looks like you're running into a problem because you aren't copying x soon enough and so you're sometimes modifying x after it's been added to permList . 似乎您遇到了问题,因为您没有足够快地复制x ,因此有时在将x添加到permList之后对其进行permList This can be solved by adding x = x[:] at the start of your while loop: 这可以通过在while循环开始时添加x = x[:]来解决:

def ourPermutations(n):
    x=list(range(1,n+1))
    permList = []
    permList+=[x]

    xcopy = x[:]
    finalPerm = xcopy[::-1]

    while x != finalPerm:
        x = x[:]
        istar = n-2
        while x[istar] > x[istar+1]:
            istar -= 1
        jstar = n-1
        while x[jstar] < x[istar]:
            jstar -= 1
        x[istar],x[jstar] = x[jstar],x[istar]
        if istar+1 == n-1:
            x = x[:]
        else:
            a = x[istar+1:]
            a = a[::-1]
            x = x[:istar+1] + a
        permList += [x]

    return permList

>>> ourPermutations(3)
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
>>> ourPermutations(4)
[[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 3, 4, 2], [1, 4, 2, 3], [1, 4, 3, 2], [2, 1, 3, 4], [2, 1, 4, 3], [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1], [3, 1, 2, 4], [3, 1, 4, 2], [3, 2, 1, 4], [3, 2, 4, 1], [3, 4, 1, 2], [3, 4, 2, 1], [
4, 1, 2, 3], [4, 1, 3, 2], [4, 2, 1, 3], [4, 2, 3, 1], [4, 3, 1, 2], [4, 3, 2, 1]]

A slightly more "pythonic" version might look like: 稍微更“ pythonic”的版本可能看起来像:

def our_permutations(n):
    x = list(range(1, n+1))
    perm_list = [x]
    final_perm = x[::-1]

    while x != final_perm:
        x = x[:]
        istar = n-2
        while x[istar] > x[istar+1]:
            istar -= 1
        jstar = n-1
        while x[jstar] < x[istar]:
            jstar -= 1
        x[istar],x[jstar] = x[jstar],x[istar]
        if istar+1 != n-1:
            a = x[istar+1:]
            a = a[::-1]
            x = x[:istar+1] + a
        perm_list += [x]

    return perm_list

Checking these functions against itertools.permutation shows that they produce the same answers, so it looks like your algorithm was correct except for that small mistake. 根据itertools.permutation对这些函数进行检查,结果表明它们产生的答案相同,因此,除了该小错误外,您的算法看起来是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 生成列表的所有排列和该列表中可能列表的排列? - Generate all permutations of a list and permutations of the possible lists within that list? 有没有办法生成项目列表的所有唯一排列 - is there a way to generate all unique permutations of a list of items 如何生成扁平列表列表的所有排列? - How to generate all permutations of the flattened list of lists? 如何生成列表的所有排列? - How do I generate all permutations of a list? 以排列值顺序生成几个列表的所有排列 - Generate All Permutations of Several Lists in Permutation-Value Order 生成列表字典的所有排列的组合(按特定顺序) - Generate combinations (in specific order) of all permutations of the dict of lists 在Python中生成列表的所有排列时,列表中元素的最大数量 - The maximum number of element in a list when generate all the permutations of a list in Python 如何生成范围内所有可能排列的列表 - How can I generate a list of all possible permutations in range Python:打印列表的所有排列,同时保持顺序和顺序 - Python: Printing all permutations of list while maintaining order and sequence 递归算法,用于在Python中生成列表长度k的所有排列 - Recursive Algorithm to generate all permutations of length k of a list in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM