How to deep copy a list?

前端 未结 8 1373
旧巷少年郎
旧巷少年郎 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]
    
    0 讨论(0)
  • 2020-11-22 03:13

    just a recursive deep copy function.

    def deepcopy(A):
        rt = []
        for elem in A:
            if isinstance(elem,list):
                rt.append(deepcopy(elem))
            else:
                rt.append(elem)
        return rt
    

    Edit: As Cfreak mentioned, this is already implemented in copy module.

    0 讨论(0)
  • 2020-11-22 03:17

    I believe a lot of programmers have run into one or two interview problems where they are asked to deep copy a linked list, however this problem is harder than it sounds!

    in python, there is a module called "copy" with two useful functions

    import copy
    copy.copy()
    copy.deepcopy()
    

    copy() is a shallow copy function, if the given argument is a compound data structure, for instance a list, then python will create another object of the same type (in this case, a new list) but for everything inside old list, only their reference is copied

    # think of it like
    newList = [elem for elem in oldlist]
    

    Intuitively, we could assume that deepcopy() would follow the same paradigm, and the only difference is that for each elem we will recursively call deepcopy, (just like the answer of mbcoder)

    but this is wrong!

    deepcopy() actually preserve the graphical structure of the original compound data:

    a = [1,2]
    b = [a,a] # there's only 1 object a
    c = deepcopy(b)
    
    # check the result
    c[0] is a # return False, a new object a' is created
    c[0] is c[1] # return True, c is [a',a'] not [a',a'']
    

    this is the tricky part, during the process of deepcopy() a hashtable(dictionary in python) is used to map: "old_object ref onto new_object ref", this prevent unnecessary duplicates and thus preserve the structure of the copied compound data

    official doc

    0 讨论(0)
  • 2020-11-22 03:17

    Here's an example of how to deep copy a 2D list:

      b = [x[:] for x in a]
    
    0 讨论(0)
  • 2020-11-22 03:18

    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

    >>> 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]]
    
    0 讨论(0)
  • 2020-11-22 03:32

    Regarding the list as a tree, the deep_copy in python can be most compactly written as

    def deep_copy(x):
        if not isinstance(x, list): return x
        else: return map(deep_copy, x)
    
    0 讨论(0)
提交回复
热议问题