错误价值观的问题”;return"-用Python编写(深度复制问题)

错误价值观的问题”;return"-用Python编写(深度复制问题),python,deep-copy,Python,Deep Copy,我用Python定义了以下函数: def step(G,V1,V2): tempcut=sys.maxint for k in range(min(len(V1),len(V2))): V1, V2, C=Switch(G,V1,V2,k) print(C) if C<tempcut: tempcut=C best1=V1 best2=V2

我用Python定义了以下函数:

def step(G,V1,V2):
    tempcut=sys.maxint
    for k in range(min(len(V1),len(V2))):
        V1, V2, C=Switch(G,V1,V2,k)
        print(C)
        if C<tempcut:
           tempcut=C
           best1=V1
           best2=V2
           print('enter')
           print(tempcut)
           print(best1)  
           print(best2)      
    return best1, best2, tempcut,  
def步骤(G、V1、V2):
tempcut=sys.maxint
对于范围内的k(最小值(len(V1),len(V2)):
V1,V2,C=开关(G,V1,V2,k)
印刷品(C)

如果C你应该复制你的列表。您正在通过引用更改它。检查

尝试:

导入副本
def步骤(G、V1、V2):
tempcut=sys.maxint
对于范围内的k(最小值(len(V1),len(V2)):
V1,V2,C=开关(G,V1,V2,k)
印刷品(C)

如果C一个更安全的结构可以做到这一点:

import copy                                 # use copy as Taha proposed
def step( G, V1, V2 ):
    tempcut = sys.maxint
    best1   = []                            # init for a case if() would never come true
    best2   = []                            # init for a case if() would never come true
    for k in range( min( len( V1 ), len( V2 ) ) ):
        V1, V2, C = Switch( G, V1, V2, k )
        print( C )
        if C < tempcut:
           tempcut = C
           best1   = copy.copy( V1 )        # use copy as Taha proposed
           best2   = copy.copy( V2 )        # use copy as Taha proposed
           print( 'enter' )
           print( tempcut )
           print( best1 )
           print( best2 )
    return ( best1, best2, tempcut,  )      # return as tuple
导入副本#使用Taha建议的副本
def步骤(G、V1、V2):
tempcut=sys.maxint
best1=[]#init表示if()永远不会实现的情况
best2=[]#init表示if()永远不会实现的情况
对于范围内的k(最小值(len(V1),len(V2)):
V1,V2,C=开关(G,V1,V2,k)
印刷品(C)
如果C
您也可以通过简单地使用
best1=V1[:]
是来获取副本!有几种解决方案可用(我将其添加为注释)。它已经被提到线程(这就是为什么我指向它)。是的!Python使用名称而不是变量。
import copy                                 # use copy as Taha proposed
def step( G, V1, V2 ):
    tempcut = sys.maxint
    best1   = []                            # init for a case if() would never come true
    best2   = []                            # init for a case if() would never come true
    for k in range( min( len( V1 ), len( V2 ) ) ):
        V1, V2, C = Switch( G, V1, V2, k )
        print( C )
        if C < tempcut:
           tempcut = C
           best1   = copy.copy( V1 )        # use copy as Taha proposed
           best2   = copy.copy( V2 )        # use copy as Taha proposed
           print( 'enter' )
           print( tempcut )
           print( best1 )
           print( best2 )
    return ( best1, best2, tempcut,  )      # return as tuple