Luckylau's Blog

你懂python吗(12)

python的拷贝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import copy
if __name__ == '__main__':
a=[1,2,3,[4,5,6]]
b=a
c=copy.copy(a) #浅拷贝
d=copy.deepcopy(a) #深拷贝
a.append(7)
a[3].append(8)
print 'a', a
print 'b', b
print 'c', c
print 'd', d
#output
a [1, 2, 3, [4, 5, 6, 8], 7]
b [1, 2, 3, [4, 5, 6, 8], 7]
c [1, 2, 3, [4, 5, 6, 8]]
d [1, 2, 3, [4, 5, 6]]
Luckylau wechat
如果对您有价值,看官可以打赏的!