相互分配变量有问题
Having problems with assigning variable to each other
我是 python 的新手,我正在尝试交换以下代码中某些变量的值:
def MutationPop(LocalBestInd,clmns,VNSdata):
import random
MutPop = []
for i in range(0,VNSdata[1]):
tmpMutPop = LocalBestInd
#generation of random numbers
RandomNums = []
while len(RandomNums) < 2:
r = random.randint(0,clmns-1)
if r not in RandomNums:
RandomNums.append(r)
RandomNums = sorted(RandomNums)
#apply swap to berths
tmpMutPop[0][RandomNums[0]] = LocalBestInd[0][RandomNums[1]]
tmpMutPop[0][RandomNums[1]] = LocalBestInd[0][RandomNums[0]]
#generation of random numbers
RandomNums = []
while len(RandomNums) < 2:
r = random.randint(0,clmns-1)
if r not in RandomNums:
RandomNums.append(r)
RandomNums = sorted(RandomNums)
#apply swap to vessels
tmpMutPop[1][RandomNums[0]] = LocalBestInd[1][RandomNums[1]]
tmpMutPop[1][RandomNums[1]] = LocalBestInd[1][RandomNums[0]]
MutPop.append(tmpMutPop)
Neighborhood = MutPop
return(Neighborhood)
我的问题是我不想改变变量“LocalBestInd
”,想用它作为参考在循环中生成新的"tmpMutPop"s,但是代码把“每次循环迭代时,LocalBestInd
”等于“tmpMutPop
”。这段代码中的其他赋值(例如 tmpMutPop[1][RandomNums[1]] = LocalBestInd[1][RandomNums[0]]
)也会出现同样的问题。
你能帮我解决这个问题吗?
谢谢
马苏德
假设 LocalBestInd
是一个列表,我认为问题在于当您设置
tmpMutPop = LocalBestInd
在循环中,值 tmpMutPop
不是一个单独的列表,而只是对实际包含数据的列表 LocalBestInd
的引用。只有一个列表 - 当您尝试更新前者时,您实际上只是在更新后者。
此处的简单示例:
>>> x = [1, 2]; y = x; y[0] = 2; print(x)
[2, 2]
可能对您有帮助的是给您的列表中的 .copy()
打电话,例如:
>>> x = [1, 2]; y = x.copy(); y[0] = 2; print(x)
[1, 2]
如果这不起作用,请查看此 SO 答案中的其他列表复制方法:How to clone or copy a list?
试试这个:
import copy
并更改行
tmpMutPop = LocalBestInd
对此:
tmpMutPop = copy.copy(LocalBestInd)
根据 LocalBestInd
的结构,您可能需要 copy.deepcopy()
。
引用 copy documentation 中的一段话来解释发生的事情:
Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.
我是 python 的新手,我正在尝试交换以下代码中某些变量的值:
def MutationPop(LocalBestInd,clmns,VNSdata):
import random
MutPop = []
for i in range(0,VNSdata[1]):
tmpMutPop = LocalBestInd
#generation of random numbers
RandomNums = []
while len(RandomNums) < 2:
r = random.randint(0,clmns-1)
if r not in RandomNums:
RandomNums.append(r)
RandomNums = sorted(RandomNums)
#apply swap to berths
tmpMutPop[0][RandomNums[0]] = LocalBestInd[0][RandomNums[1]]
tmpMutPop[0][RandomNums[1]] = LocalBestInd[0][RandomNums[0]]
#generation of random numbers
RandomNums = []
while len(RandomNums) < 2:
r = random.randint(0,clmns-1)
if r not in RandomNums:
RandomNums.append(r)
RandomNums = sorted(RandomNums)
#apply swap to vessels
tmpMutPop[1][RandomNums[0]] = LocalBestInd[1][RandomNums[1]]
tmpMutPop[1][RandomNums[1]] = LocalBestInd[1][RandomNums[0]]
MutPop.append(tmpMutPop)
Neighborhood = MutPop
return(Neighborhood)
我的问题是我不想改变变量“LocalBestInd
”,想用它作为参考在循环中生成新的"tmpMutPop"s,但是代码把“每次循环迭代时,LocalBestInd
”等于“tmpMutPop
”。这段代码中的其他赋值(例如 tmpMutPop[1][RandomNums[1]] = LocalBestInd[1][RandomNums[0]]
)也会出现同样的问题。
你能帮我解决这个问题吗?
谢谢
马苏德
假设 LocalBestInd
是一个列表,我认为问题在于当您设置
tmpMutPop = LocalBestInd
在循环中,值 tmpMutPop
不是一个单独的列表,而只是对实际包含数据的列表 LocalBestInd
的引用。只有一个列表 - 当您尝试更新前者时,您实际上只是在更新后者。
此处的简单示例:
>>> x = [1, 2]; y = x; y[0] = 2; print(x)
[2, 2]
可能对您有帮助的是给您的列表中的 .copy()
打电话,例如:
>>> x = [1, 2]; y = x.copy(); y[0] = 2; print(x)
[1, 2]
如果这不起作用,请查看此 SO 答案中的其他列表复制方法:How to clone or copy a list?
试试这个:
import copy
并更改行
tmpMutPop = LocalBestInd
对此:
tmpMutPop = copy.copy(LocalBestInd)
根据 LocalBestInd
的结构,您可能需要 copy.deepcopy()
。
引用 copy documentation 中的一段话来解释发生的事情:
Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.