如何在 while 循环中保持变量固定
How to keep variables fixed within a while loop
我有两个变量(variable_1
和 variable_2
)。它们是算法的输出,它们总是不同的,因为算法包含一些随机部分。
然后我有一个很长很复杂的函数,它将这些变量作为输入。它的基本结构是:
def function(variable_1, variable_2):
switch = True
while switch:
variable_1
variable_2
inner_function(variable_1, variable_2):
~changes variable_1 and variable_2 randomly~
~changed variable_1 and variable_2 are then transformed with data structure comprehensions.~
~in the end, there is a condition. If variable_1 and variable_2 meet this condition, switch is turned to False and the function ends. If not, the while loop shall start again, but with the original values of variable_1 and variable_2.~
函数的目的是输出变化的变量。
问题是它不起作用。如果 while 循环运行了一次迭代,并且在本次迭代结束时 switch 仍然为 True,则 variable_1
和 variable_2
不会设置回其原始值。我怎样才能做到这一点? (请记住,我不想为之前或之后的整个代码修复 variable_1
和 variable_2
)。
很抱歉没有给出一个最小可重现的例子。考虑到函数的长度和复杂性,我无法想出一个。
编辑:如果我对变量进行硬编码(这意味着我在内部函数上方写 variable_1 = "its value" 和 variable_2 = "its value",它会起作用。但我没有想做这个。
你实际上是在问:“如何按值而不是按引用传递变量”。
一种方法是将原始变量放入列表,然后从该列表创建另一个列表,然后将第二个列表作为参数传递给函数。这将模仿按值而不是按引用传递变量。
喜欢以下内容:
def function(variable_1, variable_2):
original_list = [variable_1, variable_2]
switch = True
while switch:
copy_list = original_list[:]
inner_function(copy_list):
#here you can use, change etc the data in copy_list safely
~changes variable_1 and variable_2 randomly~
~changed variable_1 and variable_2 are then transformed with data structure comprehensions.~
~in the end, there is a condition. If variable_1 and variable_2 meet this condition, switch is turned to False and the function ends. If not, the while loop shall start again, but with the original values of variable_1 and variable_2.~
所以你只需要做一个deepcopy
:
import copy
def function(variable_1o, variable_2o):
switch = True
while switch:
variable_1 = copy.deepcopy(variable_1o)
variable_2 = copy.deepcopy(variable_2o)
inner_function(variable_1, variable_2)
我有两个变量(variable_1
和 variable_2
)。它们是算法的输出,它们总是不同的,因为算法包含一些随机部分。
然后我有一个很长很复杂的函数,它将这些变量作为输入。它的基本结构是:
def function(variable_1, variable_2):
switch = True
while switch:
variable_1
variable_2
inner_function(variable_1, variable_2):
~changes variable_1 and variable_2 randomly~
~changed variable_1 and variable_2 are then transformed with data structure comprehensions.~
~in the end, there is a condition. If variable_1 and variable_2 meet this condition, switch is turned to False and the function ends. If not, the while loop shall start again, but with the original values of variable_1 and variable_2.~
函数的目的是输出变化的变量。
问题是它不起作用。如果 while 循环运行了一次迭代,并且在本次迭代结束时 switch 仍然为 True,则 variable_1
和 variable_2
不会设置回其原始值。我怎样才能做到这一点? (请记住,我不想为之前或之后的整个代码修复 variable_1
和 variable_2
)。
很抱歉没有给出一个最小可重现的例子。考虑到函数的长度和复杂性,我无法想出一个。
编辑:如果我对变量进行硬编码(这意味着我在内部函数上方写 variable_1 = "its value" 和 variable_2 = "its value",它会起作用。但我没有想做这个。
你实际上是在问:“如何按值而不是按引用传递变量”。
一种方法是将原始变量放入列表,然后从该列表创建另一个列表,然后将第二个列表作为参数传递给函数。这将模仿按值而不是按引用传递变量。
喜欢以下内容:
def function(variable_1, variable_2):
original_list = [variable_1, variable_2]
switch = True
while switch:
copy_list = original_list[:]
inner_function(copy_list):
#here you can use, change etc the data in copy_list safely
~changes variable_1 and variable_2 randomly~
~changed variable_1 and variable_2 are then transformed with data structure comprehensions.~
~in the end, there is a condition. If variable_1 and variable_2 meet this condition, switch is turned to False and the function ends. If not, the while loop shall start again, but with the original values of variable_1 and variable_2.~
所以你只需要做一个deepcopy
:
import copy
def function(variable_1o, variable_2o):
switch = True
while switch:
variable_1 = copy.deepcopy(variable_1o)
variable_2 = copy.deepcopy(variable_2o)
inner_function(variable_1, variable_2)