我正在打印我以为是列表的值,但我的输出结果是:
[...]
这代表什么?我该如何进行测试?我已经尝试过:
myVar.__repr__() != '[...]'
和
myVar.__repr_() != Ellipsis
但是没有成功...
以下是导致问题的代码精简版:
def buildPaths(graph, start, end, path=[], totalPaths=[]):
"""
returns list of all possible paths from start node to the end node
"""
path = path + [start]
if start == end:
return path
for nextNode in graph.childrenOf(start):
if nextNode not in path:
newPath = buildPaths(graph, nextNode, end, path, totalPaths)
if newPath != []: # test
totalPaths.append(newPath)
return totalPaths
totalPaths 包含很多看起来像是递归列表的内容,但我不清楚为什么。我已经在 #test 中修改了测试以防止这种情况发生。
我还尝试过:
def buildPaths(graph, thisNode, end, path=[], totalPaths=None):
"""
returns list of all possible paths from start node to the end node
"""
path = path + [thisNode]
if thisNode == end:
return path
for nextNode in graph.childrenOf(thisNode):
if nextNode not in path:
newPath = buildPaths(graph, nextNode, end, path, totalPaths)
if newPath != None:
if totalPaths == None:
totalPaths = [newPath]
else:
totalPaths.append(newPath)
return totalPaths
为了明确地返回空路径的None
。
Ellipsis
这个东西,但它可能只是显示告诉你要显示的内容比可以显示的还多。>>> [...]
->[Ellipsis]
(自Python3起)。 - anthony sottiletotalPaths.append(newPath)
时,这会修改传入的列表,因此父递归的totalPaths
也会被修改 - 然后作为newPath
返回并附加到自身。 - Izkata