python全排列最简单的实现方法

python全排列最简单的实现方法

def dfs(n,level):
    if level>0:
        for i in range(n):
            if i in stack:continue
            stack.append(i)
            dfs(n,level-1)
            stack.pop()
    else:
        print(stack)

stack = []
n = 3              #  int(input())
dfs(n,n)

# 以下是测试代码
print()
for i in range(n):
    stack.append(i)
    for j in range(n):
        if j==i:continue
        stack.append(j)
        for k in range(n):
            if k==j or k==i:continue
            stack.append(k)
            print(stack)
            stack.pop()
        stack.pop()
    stack.pop()    

李兴球

李兴球的博客是Python创意编程原创博客