How to deep copy a list?

前端 未结 8 1530
旧巷少年郎
旧巷少年郎 2020-11-22 03:07

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 =

8条回答
  •  面向向阳花
    2020-11-22 03:11

    If your list elements are immutable objects then you can use this, otherwise you have to use deepcopy from copy module.

    you can also use shortest way for deep copy a list like this.

    a = [0,1,2,3,4,5,6,7,8,9,10]
    b = a[:] #deep copying the list a and assigning it to b
    print id(a)
    20983280
    print id(b)
    12967208
    
    a[2] = 20
    print a
    [0, 1, 20, 3, 4, 5, 6, 7, 8, 9,10]
    print b
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10]
    

提交回复
热议问题