在Python中深入复制列表 [英] Deep copy a list in Python

查看:149
本文介绍了在Python中深入复制列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表副本的问题:



所以,当我从<$ c $ <$ c $>获得 E0 c>'get_edge',我通过调用'E0_copy = list(E0)'创建 E0 code>。这里我猜想 E0_copy E0 的深度副本,我通过 E0_copy into 'karger(E)'。但是在主函数中。

为什么在for循环之前,'的结果打印E0 [1:10]'之后的for循环?



以下是我的代码:

  def get_graph b $ bf = open('kargerMinCut.txt')
G = {}
for line in f:
ints = [int(x)for x in line.split b $ b G [ints [0]] = ints [1:len(ints]]
return G

def 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 E

def 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
如果E [i] [0] == v1:
E [i] [0] = v0
如果E [i ] [1] == v1:
E [i] [1] = v0
if E [i] [0] == E [i] [1]:
(i)
i- = 1
i + = 1

mincut = len(E)
return mincut


如果__name __ ==__ main__:
import copy
G = get_graph()
results = []
E0 = get_edge(G)
print E0 [1: 10] ##这个结果不等于print2
在范围(1,5)中的k:
E0_copy = list(E0)##我想这里E0_coypy是E0的深拷贝
results.append(karger(E0_copy))
#print结果是%d%min(results)
print E0 [1:10] ##这是print2



提前感谢!

解决方案

p> E0_copy 不是深拷贝。您不使用 list()(两个 list(...) testList [:] 是浅拷贝)。



您可以使用 copy.deepcopy(...) 深入复制列表。

  deepcopy(x,memo = None,_nil = [])
对任意Python对象进行深度复制操作。

请参阅以下代码段 -

 >>> 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的更改 - >不是deepcopy。
[[1,10,3],[4,5,6]]

现在查看 deepcopy 操作

 >> 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不改变 - >深复制
[[1,10,3],[4,5,6]]


I have some problem with a List copy:

So After I got E0 from 'get_edge', I make a copy of E0 by calling 'E0_copy = list(E0)'. Here I guess E0_copy is a deep copy of E0, and I pass E0_copy into 'karger(E)'. But in the main function.
Why does the result of 'print E0[1:10]' before the for loop is not the same with that after the for loop?

Below is my code:

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 G

def 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 E

def 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 mincut


if __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

Thanks in advance!

解决方案

E0_copy is not a deep copy. You don't make a deep copy using list() (Both list(...) and testList[:] are shallow copies).

You use copy.deepcopy(...) for deep copying a list.

deepcopy(x, memo=None, _nil=[])
    Deep copy operation on arbitrary Python objects.

See the following snippet -

>>> 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]]

Now see the deepcopy operation

>>> 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]]

这篇关于在Python中深入复制列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆