美文网首页
shallow VS deep copy

shallow VS deep copy

作者: 好小葱1 | 来源:发表于2018-07-31 11:50 被阅读5次

Copying a list

完全可以将python赋值理解成改变指针指向的操作。

>>> colours1 = ["red", "green"]
>>> colours2 = colours1
>>> colours2 = ["rouge", "vert"]
>>> print colours1
['red', 'green']
deep_copy_1.png
>>> colours1 = ["red", "green"]
>>> colours2 = colours1
>>> colours2[1] = "blue"
>>> colours1
['red', 'blue']
deep_copy_2.png

Copy with the Slice Operator

>>> list1 = ['a','b','c','d']
>>> list2 = list1[:]
>>> list2[1] = 'x'
>>> print list2
['a', 'x', 'c', 'd']
>>> print list1
['a', 'b', 'c', 'd']
>>> 
deep_copy_3.png
>>> lst1 = ['a','b',['ab','ba']]
>>> lst2 = lst1[:]
>>> lst1 = ['a','b',['ab','ba']]
>>> lst2 = lst1[:]
>>> lst2[0] = 'c'
>>> lst2[2][1] = 'd'
>>> print(lst1)
['a', 'b', ['ab', 'd']]
deep_copy_4.png

Using the Method deepcopy from the Module copy

from copy import deepcopy

lst1 = ['a','b',['ab','ba']]

lst2 = deepcopy(lst1)

lst2[2][1] = "d"
lst2[0] = "c";

print lst2
print lst1
$ python deep_copy.py 
['c', 'b', ['ab', 'd']]
['a', 'b', ['ab', 'ba']]
deep_copy_5.png

相关文章

  • shallow VS deep copy

    Copying a list 完全可以将python的赋值理解成改变指针指向的操作。 Copy with the ...

  • Java------List的深拷贝与浅拷贝

    Java的浅拷贝(Shallow Copy)、深拷贝(Deep Copy)。 浅拷贝(Shallow Copy) ...

  • OC基础

    Difference between shallow copy and deep copy?1> 浅拷贝:指...

  • copy与deepcopy的区别

    python中有一个copy模块,他提供shallow copy与deep copy两种方式,shallow co...

  • 转载:[题]

    Difference between shallow copy and deep copy?1> 浅拷贝:指针(地...

  • shallow copy & deep copy

    浅拷贝:只复制指向对象的指针,而不复制引用对象本身。在另一个地方用同一个指针引用该对象。 深拷贝:复制引用对象本身...

  • Shallow Copy & Deep Copy

    对象的拷贝需要首先实现协议浅拷贝:object A和ob...

  • IOS 常见面试题

    1.Difference between shallow copy and deep copy? 浅复制和深复制的...

  • 集锦2 iOS面试

    1.Difference between shallow copy and deep copy? 浅复制和深复制的...

  • 集锦1 iOS面试

    iOS面试题锦集 1.Difference between shallow copy and deep copy?...

网友评论

      本文标题:shallow VS deep copy

      本文链接:https://www.haomeiwen.com/subject/hyefvftx.html