python深度复制列表_Python-如何深层复制列表?
我對列表副本有一些問題:
所以之后我得到了E0來自'get_edge',我做的副本E0通過調用'E0_copy = list(E0)'。我猜這里E0_copy是的較深的副本E0,我通過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 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
總結
以上是生活随笔為你收集整理的python深度复制列表_Python-如何深层复制列表?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML5制作斑马线表格,JavaScr
- 下一篇: python一只青蛙一次可以_青蛙王子