Python List assignment methods, can not be directly equal

Python List assignment method

Disclaimer: This article is a blogger original article, follow the  CC 4.0 BY-SA  copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/lovelyaiq/article/details/55102518

  Python object copy regarding use of three types, assignment, shallow copy and deep copy. They are different but linked, just recently encountered this type of problem under study.

First, the assignment

        In python, assignment object is a simple object references, unlike point and C ++. as follows:        

 

  1.  
    a = [ 1,2,3,"hello",["python","C++"]]
  2.  
    b = a
  3.  
    print a==b #True
        In this case, b and a are the same, they point to the same piece of memory, b but a alias, is quoted. We can use a and b are the same judge, returns True, they show the same address, the same content.

        Assignment operator (including objects as parameters, return values) will not open up new memory space, it just copy a reference to the new object. In other words, in addition to b the name, no other memory overhead.

        Modified a, b on the impact; Similarly, the modification affected the b a.

 

  1.  
    a = [ 1,2,3,"hello",["python","C++"]]
  2.  
    b = a
  3.  
    b. append("ADD")
  4.  
    print "a=",a,"b=",b
#a=[1, 2, 3, 'hello', ['python', 'C++'], 'ADD'] b=[1, 2, 3, 'hello', ['python', 'C++'], 'ADD']

Second, the shallow copy (shallow copy)

        Shallow copy creates a new object whose content is a reference to the original object.

        Shallow copy of three forms: a slice operation, plant function, copy function copy module. The above-described example a:        

        1, slicing: b = a [:] or b = [each for each in a]

        2, factory function: b = list (a)

        3、copy函数:b = copy.copy(a)

        b shallow copy of the produce is no longer a use is that they can not find the same object, use the id to view, find that they do not point to the same piece of memory. However, when we use the id (x) for x in a and id (x) for x in b, the address can be seen that both contain the same element.

        In this case, a and b are different, Modify b theoretically does not affect a. For example b.append ([4,5]).

 

  1.  
    a = [ 1,2,3,"hello",["python","C++"]]
  2.  
    b = a[:]
  3.  
    b. append("ADD")
  4.  
    print "a",a,"b",b
#a [1, 2, 3, 'hello', ['python', 'C++']] b [1, 2, 3, 'hello', ['python', 'C++'], 'ADD']

        Note, however, a shallow copy is called a shallow copy, it is only just copy a layer, there is a nested list in a, if we modify it, the situation is different.

        a [4] .append ( "C"). View b, b you will find also changed. This is because, you change the nested list. Modify the outer element, will modify its reference, so that they point to a different location, modify elements nested list, address list and to change, point is the same location.

Third, the deep copy (deep copy)

        Only one form deep copy, copy module deepcopy function.

        And copy the corresponding shallow, deep copy copy all elements of the objects, including multiple nested elements. Thus, it's time and space overhead is higher.

        Similarly to la, when using b = copy.deepcopy (a), then b modification will not affect the a. Even nested list has a deeper level, it will not have any effect, because deep copy of the object is simply out of a brand new object, no longer have any connection with the original object.

Fourth, the warning about the copy operation

        1, for non-container type, such as numbers, characters, and other types of "atom", a copy of said no. References are generated by the original object.

        2, if the tuple type object atoms containing variable values, even if a deep copy, only to give a pale copy.

Guess you like

Origin www.cnblogs.com/wanghaolu/p/11528453.html