函数返回空,循环内打印需要输出

时间:2015-04-21 18:15:17

标签: python function

所以我有一个读取字典的函数:

{'a': ['a', 'b'], 'c': ['c', 'a'], 'b': ['b', 'c']}

我的功能看起来像

def unioncompare(node, starting):
  visited, stack = set(), [starting]
  while stack:
    vertex = stack.pop()
    if vertex not in visited:
      visited.add(vertex)
      stack.extend(node[vertex])
  return stack

它健康会返回一个空列表

但是在简单的调试中,如果我在倒数第二行(在if语句中)插入一个print语句,那么迭代会产生(大致)我之后的内容

 if vertex not in visited:
    visited.add(vertex)
    stack.extend(node[vertex])
    print stack
 return stack

['b', 'c']
['b', 'c', 'a']
['b', 'c', 'a', 'b']
[]

我没有严格理解if语句中的值发生了什么,它们没有被传递出去并正确返回。

为任何帮助干杯!

0 个答案:

没有答案
相关问题