Python-在递归函数中使用共享变量 [英] Python - using a shared variable in a recursive function
问题描述
我正在使用递归函数在Python中对列表进行排序,并且我想在函数继续时跟踪排序/合并的数量.但是,当我在函数内部声明/初始化变量时,它在函数的每个后续调用中都变成局部变量.如果我在函数外部声明该变量,则该函数会认为该变量不存在(即无法访问它).如何在函数的不同调用之间共享该值?
I'm using a recursive function to sort a list in Python, and I want to keep track of the number of sorts/merges as the function continues. However, when I declare/initialize the variable inside the function, it becomes a local variable inside each successive call of the function. If I declare the variable outside the function, the function thinks it doesn't exist (i.e. has no access to it). How can I share this value across different calls of the function?
我试图在函数内部和外部使用全局"变量标签,如下所示:
I tried to use the "global" variable tag inside and outside the function like this:
global invcount ## I tried here, with and without the global tag
def inv_sort (listIn):
global invcount ## and here, with and without the global tag
if (invcount == undefined): ## can't figure this part out
invcount = 0
#do stuff
但是我无法弄清楚如何检查全局变量的未定义状态,并在第一次递归调用时为其赋予一个值(因为在所有后续递归中,它都应具有一个值并被定义).
But I cannot figure out how to check for the undefined status of the global variable and give it a value on the first recursion call (because on all successive recursions it should have a value and be defined).
我的第一个想法是从函数的每次调用中返回变量,但是我不知道如何将两个对象从函数中传递出去,并且我已经必须将列表传递出去以进行递归排序工作.解决该问题的第二次尝试涉及将变量invcount
添加到我要传递的列表中,作为具有标识符的最后一个元素,例如"i27"
.然后,我可以检查最后一个元素中是否存在标识符(在此示例中为字母i
),如果存在,则pop()在函数调用的开始时将其关闭,然后在递归期间将其重新添加.在实践中,这确实变得令人费解,尽管最终可能会奏效,但我想知道是否有更实用或更简单的解决方案.
My first thought was to return the variable out of each call of the function, but I can't figure out how to pass two objects out of the function, and I already have to pass the list out for the recursion sort to work. My second attempt to resolve this issue involved me adding the variable invcount
to the list I'm passing as the last element with an identifier, like "i27"
. Then I could just check for the presence of the identifier (the letter i
in this example) in the last element and if present pop() it off at the beginning of the function call and re-add it during the recursion. In practice this is becoming really convoluted and while it may work eventually, I'm wondering if there is a more practical or easier solution.
是否可以在不直接传递/返回变量的情况下共享变量?
Is there a way to share a variable without directly passing/returning it?
推荐答案
您可以做几件事.以您的示例为例,您应该像这样修改它:
There's couple of things you can do. Taking your example you should modify it like this:
invcount = 0
def inv_sort (listIn):
global invcount
invcount += 1
# do stuff
但是这种方法意味着您应该在每次调用inv_sort
之前将invcount
设置为零.
因此,实际上最好返回invcount
作为结果的一部分.例如,使用这样的元组:
But this approach means that you should zero invcount
before each call to inv_sort
.
So actually its better to return invcount
as a part of result. For example using tuples like this:
def inv_sort(listIn):
#somewhere in your code recursive call
recursive_result, recursive_invcount = inv_sort(argument)
# this_call_invcount includes recursive_invcount
return this_call_result, this_call_invcount
这篇关于Python-在递归函数中使用共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!