相关疑难解决方法(0)

如何深层复制列表?

我对List副本有一些问题:

所以我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 …
Run Code Online (Sandbox Code Playgroud)

python copy list deep-copy

111
推荐指数
5
解决办法
19万
查看次数

int 的 Python 范围与列表的范围

我在理解 Python 中的范围概念时遇到了麻烦。当我使用以下代码时:|

x = 100

def printer(x):
    x = 25
    print(x)

printer(x) -> prints 25
print(x) -> prints 100
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我理解变量“x”在函数中只有 25 作为值。当我尝试对列表做同样的事情时,我的输出是不同的。见下面的代码:

board = ['','']

def printer():
    board[0] = 'X'
    print(board)

printer() --> prints ['X', '']
print(board) --> also prints ['X', ''] || Here I was expecting to print ['','']
Run Code Online (Sandbox Code Playgroud)

python python-3.x

-2
推荐指数
1
解决办法
216
查看次数

标签 统计

python ×2

copy ×1

deep-copy ×1

list ×1

python-3.x ×1