如何深抄一份清单?

如何深抄一份清单?

我对一份清单副本有一些问题:

所以在我得到E0从…'get_edge',我复印了一份E0打电话'E0_copy = list(E0)'..在这里我猜E0_copyE0,我过去了E0_copy'karger(E)'..但在主要功能上。
为什么'print E0[1:10]'在for循环之前与for循环之后的不一样吗?

下面是我的代码:

def get_graph():
    f=open('kargerMinCut.txt')
    G={}
    for line in f:
        ints = [int(x) for x in line.split()]
        G[ints[0]]=ints[1:len(ints)]
    return Gdef get_edge(G):
    E=[]
    for i in range(1,201):
        for v in G[i]:
            if v>i:
                E.append([i,v])
    print id(E)
    return Edef karger(E):
    import random
    count=200 
    while 1:
        if count == 2:
            break
        edge = random.randint(0,len(E)-1)
        v0=E[edge][0]
        v1=E[edge][1]                   
        E.pop(edge)
        if v0 != v1:
            count -= 1
            i=0
            while 1:
                if i == len(E):
                    break
                if E[i][0] == v1:
                    E[i][0] = v0                if E[i][1] == v1:
                    E[i][1] = v0                if E[i][0] == E[i][1]:
                    E.pop(i)
                    i-=1
                i+=1

    mincut=len(E)
    return mincutif __name__=="__main__":
    import copy
    G = get_graph()
    results=[]
    E0 = get_edge(G)
    print E0[1:10]               ## this result is not equal to print2
    for k in range(1,5):
        E0_copy=list(E0)         ## I guess here E0_coypy is a deep copy of E0
        results.append(karger(E0_copy))
       #print "the result is %d" %min(results)
    print E0[1:10]               ## this is print2


蝴蝶刀刀
浏览 503回答 3
3回答

慕田峪7331174

E0_copy不是很深的拷贝。你不会用list()(两者均list(...)和testList[:]都是浅薄的复制品)。你用copy.deepcopy(...)为了更深的复制一份清单。deepcopy(x, memo=None, _nil=[])    Deep copy operation on arbitrary Python objects.见以下片段->>> a = [[1, 2, 3], [4, 5, 6]]>>> b = list(a)>>> a[[1, 2, 3], [4, 5, 6]]>>> b[[1, 2, 3], [4, 5, 6]]>>> a[0][1] = 10>>> a[[1, 10, 3], [4, 5, 6]]>>> b   # b changes too -> Not a deepcopy.[[1, 10, 3], [4, 5, 6]]现在看deepcopy操作>>> import copy>>> b = copy.deepcopy(a)>>> a[[1, 10, 3], [4, 5, 6]]>>> b[[1, 10, 3], [4, 5, 6]]>>> a[0][1] = 9>>> a[[1, 9, 3], [4, 5, 6]]>>> b    # b doesn't change -> Deep Copy[[1, 10, 3], [4, 5, 6]]

慕尼黑8549860

我相信很多程序员遇到了一两个面试问题,他们被要求深入复制链接列表,但是这个问题并不像听起来那么容易!在python中,有一个叫做“复制”的模块,它有两个有用的函数。import copy copy.copy()copy.deepcopy()Copy()是一个浅拷贝函数,如果给定的参数是一个复合数据结构,例如列单,然后python将创建另一个相同类型的对象(在本例中,新名单)但对于旧列表中的所有内容,只有它们的引用被复制# think of it likenewList = [elem for elem in oldlist]从直觉上讲,我们可以假设深度复制()将遵循相同的范式,唯一的区别是对于每个我们将递归调用深度复制,(就像mbcoder的回答一样)但这是错误的!深度复制()实际上保留了原始复合数据的图形结构:a = [1,2]b = [a,a] # there's only 1 object ac = deepcopy(b)# check the resultc[0] is a  # return False, a new object a' is createdc[0] is c[1] # return True, c is [a',a'] not [a',a'']这是一个棘手的部分,在深入复制()的过程中,使用hashtable(python中的字典)映射:“old_objectref到new_objectref”,这样可以防止不必要的重复,从而保留复制的复合数据的结构。官方医生

ABOUTYOU

如果列表的内容是基本数据类型,则可以使用理解。new_list = [i for i in old_list]您可以将其嵌套到多维列表中,如:new_grid = [[i for i in row] for row in grid]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python