递归重复输出-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: