Changing list elements in shallow copy

后端 未结 3 401
闹比i
闹比i 2020-12-20 07:16

I have one question about list shallow copy.

In both examples, I modified one element of the list, but in example 1, list b changed, while in example 2,

相关标签:
3条回答
  • 2020-12-20 07:59

    [before changing valuesafter changing values1. = copies the reference of object, hence any changes in either list, reflects in another

    1. b=list(a) or b=a.copy() -> do the same work. That is it copies the reference of only the individual objects i.e like b[0]=a[0] and b2=a2 and so on. With int, string etc, it's like if x = 10 and y = x and changing the value of 'x' or 'y' won't affect the other. This is what happens for the remaining elements of the a and b when you do a shallow copy.

    So as in your question when doing b=list(a) and a[1]=0 using a shallow copy behaves as explained above and hence the changes are not reflected in both the list . But the nested listed acts as list assignment like a=[1,2,3] and b=a and making a2=3 will change b2 to 3 as well, i.e.changes in a or b effect both (same as in case 1 above). So this is why in case of a list with in a list any changes reflects in both the list. As in your example doing d=list(c) (here when copying d[2]=c[2] this is similar to list assignment i.e. the reference is copied and in case of list assignment changes are reflected in both so changes to d2 or c2 is reflected in both list) so doing c[2][0] = 0 will also change d[2][0] to zero.

    Try the code at http://www.pythontutor.com/visualize.html#mode=edit to understand better

    a=[1,2,"hello",[3,4],5]
    b=a
    c=a.copy()
    a[0]=2
    a[3][0]=6
    
    0 讨论(0)
  • 2020-12-20 08:10

    In the both examples, you are creating a shallow copy of the list. The shallow copies essentially copies the aliases to all elements in the first list to the second list.

    So you have copied the reference to an [int, int, list, int]. The int elements are immutable, but the list element is mutable. So the third elements both point to the same object in Python's memory. Modifying that object modifies all references to it.

    0 讨论(0)
  • A shallow copy means that you get a new list but the elements are the same. So both lists have the same first element, second element, etc.

    If you add, remove, or replace a value from the shallow copied list that change is not reflected in the original (and vise-versa) because the shallow copy created a new list. However if you change an element in either that change is visible in both because both lists reference the same item. So the inner list is actually shared between both the new list and the old list and if you change it, that change is visible in both.

    Note that you actually didn't change an element in either example, you replace an element of the list in the first example and in the second example, you replace an element of an element of your list.

    I'm currently using graphviz a lot so let me add some images to illustrate this:

    The shallow copy means you get a new list but the objects stored in the list are the same:

    If you replace an element in any of these the corresponding element will just reference a new item (your first example). See how one list references the two and the other the zero:

    While a change to an referenced item will change that item and every object that references that item will see the change:

    0 讨论(0)
提交回复
热议问题