简体   繁体   English

Python中输入数字列表的排列

[英]Permutations of a list of input numbers in Python

I'm supposed to write a program in Python that will get a list of numbers from the user until the user inputs 0 , then print all the permutations of this list of numbers. 我应该用Python编写一个程序,该程序将从用户那里获取数字列表,直到用户输入0 ,然后打印此数字列表的所有排列。 I was working on the code, which seemed right at the begining, but for some reason I'm getting weird output. 我正在编写代码,一开始似乎很正确,但是由于某种原因,我得到了奇怪的输出。

It seems like the list is static, and every time the function returns it adds objects to the latest list. 列表似乎是静态的,并且每次函数返回时,它都会将对象添加到最新列表中。 This doesn't happen the way it was before the function was called recursively. 这与递归调用函数之前的情况不同。

Here is what I have so far: 这是我到目前为止的内容:

    def permutation(numberList,array,place):
        if (place==len(numberList)):
            print array
        else:
            x=0
            while (x < len(numberList)):
                array.append(numberList[x])
                permutation(numberList,array,place+1)
                x+=1

    def scanList():
        numberList=[];
        number=input()
        #keep scanning for numbers for the list
        while(number!=0):
           numberList.append(number)
           number=input()
        return numberList

permutation(scanList(),[],0)

This is the output for the input 1 2 3 0 , for example: 这是输入1 2 3 0的输出,例如:

[1, 1, 1]
[1, 1, 1, 2]
[1, 1, 1, 2, 3]
[1, 1, 1, 2, 3, 2, 1]
[1, 1, 1, 2, 3, 2, 1, 2]
[1, 1, 1, 2, 3, 2, 1, 2, 3]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2, 3]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2, 3, 2, 1]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3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[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 3, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 3, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3]

I would appreciate any help. 我将不胜感激任何帮助。

The Python way is to use itertools. Python的方法是使用itertools。

from itertools import permutations
for permutation in permutations([1,2,3]):
    print(permutation)

Now whats wrong with your algorithm. 现在,您的算法出了什么问题。 As you noticed, the list is static (well not really, but your using the same list every time) 如您所见,该列表是静态的(不是真的,但是您每次都使用相同的列表)

A simple fix would be to copy the list each time. 一个简单的解决方法是每次复制列表。

def permutation(numberList,array,place):
    if (place==len(numberList)):
        print array
    else:
        x=0
        while (x < len(numberList)):
            array2 = array[:] // here happens the copy
            array2.append(numberList[x])
            permutation(numberList,array2,place+1)
            x+=1

The thing is, the list [] in Python is dynamic. 事实是,Python中的list []是动态的。 So when you add elements to it with array.append(numberList[x]) , they stay there forever. 因此,当您使用array.append(numberList[x])向其中添加元素时,它们将永远存在。 Just remove the added element after your recursive call: 只需在递归调用之后删除添加的元素:

def permutation(numberList,array,place):
    if (place==len(numberList)):
        print(array)
    else:
        x=0
        while (x < len(numberList)):
            array.append(numberList[x])
            permutation(numberList,array,place+1)
            array.pop()
            x+=1

That's actually the common way to write depth-first search algorithms: modify your structure, make recursive call, undo modifications. 实际上,这是编写深度优先搜索算法的常用方法:修改结构,进行递归调用,撤消修改。 The result of your program doesn't seem to be permutations of the input though. 程序的结果似乎并不是输入的排列

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM