我编写了以下递归程序来计算两个集合的叉积。
def combine(input1,input2,output):
if len(input2)==0:
return output
else:
for num in input1:
output.append((num,input2[0]))
combine(input1,input2[1:],output)
input1=[1 2 5]
input2=[2 3]
output=[(1,2), (1,3), (2,2),(2,3),(5,2),(5,3)]
有没有可能使递归更好,例如删除else中的循环并尝试在同一函数中完成。我正在寻找解决问题的不同方法。
编辑:不要使用内置功能解决问题。寻找如何以不同方式进行递归,并且不使用itertools.product。
itertools.product
吗? - Pavel Anossovcombine
调用需要在前面加上return
。 - DSM