python list copy实例

python list copy

测试代码

import copy
a = [1,2,3,['a','b']]
b = a
c = a[:]
d = copy.copy(a)
e = copy.deepcopy(a)

print "a         ",a
print "=         ",b
print "[:]         ",c
print "copy      ",d
print "deepcopy ",e
print 
 
a[0] = 9
print "a         ",a
print "=         ",b
print "[:]         ",c
print "copy      ",d
print "deepcopy ",e
print 
a[-1][0] = 'x'
 
print "a         ",a
print "=         ",b
print "[:]         ",c
print "copy      ",d
print "deepcopy ",e

输出:

a          [1, 2, 3, ['a', 'b']]
=          [1, 2, 3, ['a', 'b']]
[:]          [1, 2, 3, ['a', 'b']]
copy       [1, 2, 3, ['a', 'b']]
deepcopy  [1, 2, 3, ['a', 'b']]
 
a          [9, 2, 3, ['a', 'b']]
=          [9, 2, 3, ['a', 'b']]
[:]          [1, 2, 3, ['a', 'b']]
copy       [1, 2, 3, ['a', 'b']]
deepcopy  [1, 2, 3, ['a', 'b']]
 
a          [9, 2, 3, ['x', 'b']]
=          [9, 2, 3, ['x', 'b']]
[:]          [1, 2, 3, ['x', 'b']]
copy       [1, 2, 3, ['x', 'b']]
deepcopy  [1, 2, 3, ['a', 'b']]
    原文作者:Rick_Ji
    原文地址: https://www.jianshu.com/p/1490eced55de
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞