Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
递归重复输出-python 3_Python_Recursion - Fatal编程技术网

递归重复输出-python 3

递归重复输出-python 3,python,recursion,Python,Recursion,我正在尝试递归,遇到了麻烦 代码如下: inputs = [] def ins(x,y,length): global inputs, j if [x,y] not in inputs: inputs.append([x,y]) if x is not length: ins(x+1,y,length) if y is not length: ins(x,y+1,length) j = 0 ins(0,0,12)

我正在尝试递归,遇到了麻烦

代码如下:

inputs = []
def ins(x,y,length):
    global inputs, j
    if [x,y] not in inputs:
        inputs.append([x,y])
    if x is not length:
        ins(x+1,y,length)
    if y is not length:
        ins(x,y+1,length)
j = 0
ins(0,0,12)

但它似乎对同一个x,y组合进行了多次检查,并给出了一个10400599个项目的输入,而不是169个项目。我的糟糕,我所要做的就是改变:

if y is not length:
致:


尝试用
替换
不是
=无复制-这在Py2.7和Python3.6中都可以正常工作。是的,它可以正常工作,但需要更长的时间,并重复x,y组合,从而产生很长的输入,而不是很短的输入。
elif y is not length: