python - 递归 - Python - 为什么 print(Foo) 显示的内容与 print(FunctionReturningFoo()) 不同
问题描述
为什么 print(foo) 显示的内容与 print(functionreturningthesame()) 不同
查看输出,在函数中打印数组显示正确答案,但打印函数的返回没有。我可能对递归迭代感到困惑......
def AreNotOpposite(a,b):
if a == "NORTH" and b == "SOUTH":
return False
if a == "SOUTH" and b == "NORTH":
return False
if a == "WEST" and b == "EAST":
return False
if a == "EAST" and b == "WEST":
return False
return True
def canBeBetter(arr):
for i in range(len(arr)-1):
if not AreNotOpposite(arr[i],arr[i+1]):
return True
return False
def dirReduc(arr):
re = []
avoid = -1
for i in range(len(arr)):
if avoid == i:
continue
if i+1 == len(arr):
re.append(arr[i])
elif AreNotOpposite(arr[i],arr[i+1]):
re.append(arr[i])
else: #do not append neither the nextone
avoid = i+1
if canBeBetter(re): #can reduce more?
dirReduc(re)
else:
print(re)
return re
print (dirReduc(['NORTH', 'WEST', 'EAST','SOUTH', 'NORTH','SOUTH','EAST','NORTH']))
输出:
['EAST', 'NORTH']
None
解决方案
我想你想要这样的东西
if canBeBetter(re): #can reduce more?
return dirReduc(re)
else:
return re
推荐阅读
- java - 如何使用 GroovyShell 传递 JVM 参数和脚本参数?
- java - SimpleJavaMail:dkimPrivateKey 来自哪里?
- firebase - 在颤振中使用图像选择器时发现方法异常
- python - 在 Neuraxle 中可以使用 sklearn 和 pytorch 的 n_jobs > 1 吗?
- java - 如何在 gradle 4.5 中使用 junit5
- c++ - 我的 Perl XS 代码中的内存泄漏在哪里?
- vue.js - 如何在 Vue js 打印中包含 Vuetify css?
- azure - Azure B2C中如何使用Microsoft Graph查询用户权限来源
- c++ - 将对象存储在向量中时的不良行为
- sql - 确定 SQL 中的时间间隔