Python 递归函数的序列/堆栈图
有关以下代码:Python 递归函数的序列/堆栈图,python,string,recursion,sequence-diagram,Python,String,Recursion,Sequence Diagram,有关以下代码: def printList(L): if L: print L[0] printList(L[1:]) 我可以有这样的序列图: # NON PYTHON PSEUDO CODE PrintList([1,2,3]) prints [1,2,3][0] => 1 runs printList([1,2,3][1:]) => printList([2,3]) => we're now in printLi
def printList(L):
if L:
print L[0]
printList(L[1:])
我可以有这样的序列图:
# NON PYTHON PSEUDO CODE
PrintList([1,2,3])
prints [1,2,3][0] => 1
runs printList([1,2,3][1:]) => printList([2,3])
=> we're now in printList([2,3])
prints [2,3][0] => 2
runs printList([2,3][1:]) => printList([3])
=> we are now in printList([3])
prints [3][0] => 3
runs printList([3][1:]) => printList([])
=> we are now in printList([])
"if L" is false for an empty list, so we return None
=> we are back in printList([3])
it reaches the end of the function and returns None
=> we are back in printList([2,3])
it reaches the end of the function and returns None
=> we are back in printList([1,2,3])
it reaches the end of the function and returns None
所以我的问题是,如果我将代码更改为:
def printList(L):
if L:
print L[0]
printList(L[1:])
print L[0]
序列图将如何更改,我想了解在执行此代码的过程中到底发生了什么。递归调用后调用的print语句都将在“备份过程中”被命中。也就是说,您的每个语句:“它到达函数末尾并返回None”都可以更改为“它打印L[0]的当前值,到达函数末尾并返回None”,分别为3、2和1 像这样:
PrintList([1,2,3])
prints [1,2,3][0] => 1
runs printList([1,2,3][1:]) => printList([2,3])
=> we're now in printList([2,3])
prints [2,3][0] => 2
runs printList([2,3][1:]) => printList([3])
=> we are now in printList([3])
prints [3][0] => 3
runs printList([3][1:]) => printList([])
=> we are now in printList([])
"if L" is false for an empty list, so we return None
=> we are back in printList([3])
prints [3][0] => 3
it reaches the end of the function and returns None
=> we are back in printList([2,3])
prints [2,3][0] => 2
it reaches the end of the function and returns None
=> we are back in printList([1,2,3])
prints [1,2,3][0] => 1
it reaches the end of the function and returns None
递归调用之后调用的print语句都将在“备份过程中”被命中。也就是说,您的每个语句:“它到达函数末尾并返回None”都可以更改为“它打印L[0]的当前值,到达函数末尾并返回None”,分别为3、2和1 像这样:
PrintList([1,2,3])
prints [1,2,3][0] => 1
runs printList([1,2,3][1:]) => printList([2,3])
=> we're now in printList([2,3])
prints [2,3][0] => 2
runs printList([2,3][1:]) => printList([3])
=> we are now in printList([3])
prints [3][0] => 3
runs printList([3][1:]) => printList([])
=> we are now in printList([])
"if L" is false for an empty list, so we return None
=> we are back in printList([3])
prints [3][0] => 3
it reaches the end of the function and returns None
=> we are back in printList([2,3])
prints [2,3][0] => 2
it reaches the end of the function and returns None
=> we are back in printList([1,2,3])
prints [1,2,3][0] => 1
it reaches the end of the function and returns None
你明白你贴的序列图吗?你试过绘制新代码的图表吗?@Karl Knechtel:是的,我理解我的图表,但我不知道python是先执行所有的打印列表(L[1:]),然后执行最后一个打印列表(L[0]),还是在每个打印列表(L[1:])之后执行它……你确定你理解这个图表吗?我当然不明白。你明白你贴的序列图吗?你试过绘制新代码的图表吗?@Karl Knechtel:是的,我理解我的图表,但我不知道python是先执行所有的打印列表(L[1:]),然后执行最后一个打印列表(L[0]),还是在每个打印列表(L[1:])之后执行它……你确定你理解这个图表吗?我当然不知道。