python之深浅拷贝

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python之深浅拷贝相关的知识,希望对你有一定的参考价值。

今天来给大家讲一下深浅拷贝,深浅拷贝需要用到copy模块,这里需要导入copy模块

import copy

 

今天的博客结构是这样的,先对字符串和数字这两种类分别使用赋值、浅拷贝、深拷贝

1、首先来看下赋值的方法

a1 = "abc"
a2 = "123"
b1 = a1
b2 = a2

print(id(a1),id(b1),sep="/",end="\n")
# 27299360/27299360
print(id(a2),id(b2),sep="/",end="\n")
# 30486656/30486656
b1 = "add"
b2 = "789"
print(b1,a1)
# add abc
print(b2,a2)
# 789 123

2、在来看下浅拷贝的方法

a3 = copy.copy(a1)
a4 = copy.copy(a2)
print(id(a3),id(a1),sep="/",end="\n")
# 7179808/7179808
print(id(a4),id(a2),sep="/",end="\n")
# 7876736/7876736

a3 = "had"
a4 = "678"
print(a3,a1)
# had abc
print(a4,a2)
# 678 123

 

3、最后在看下深拷贝的方法

a5 = copy.deepcopy(a1)
a6 = copy.deepcopy(a2)
print(id(a5),id(a1),sep="/",end="\n")
# 26840608/26840608
print(id(a6),id(a2),sep="/",end="\n")
# 27537536/27537536

a5 = "def"
a6 = "456"
print(a5,a1)
# def abc
print(a6,a2)
# 456 123

 

结论:对于字符串和数字无论是赋值,浅拷贝,深拷贝,均对原来的变量没有影响

 

在来看下赋值,浅拷贝,深拷贝对列表和字典的影响,其实上述对list和dict的影响是一样的

首先我们用list来举例,先说下一个没有嵌套的list

1、先说下赋值

l1 = ["a","b","c"]

l2 = l1
print(id(l1),id(l2),sep="/",end="\n")
# 30744488/30744488

l2.append("d")
print(l2,l1,sep="/",end="\n")
# [‘a‘, ‘b‘, ‘c‘, ‘d‘]/[‘a‘, ‘b‘, ‘c‘, ‘d‘]
print(id(l1),id(l2),sep="/",end="\n")
# 28516264/28516264

2、在来看下浅拷贝

l1 = ["a","b","c"]
l2 = copy.copy(l1)
print(id(l1),id(l2),sep="/",end="\n")
# 30892240/30893000
l2.append("d")
print(l1,l2)
# [‘a‘, ‘b‘, ‘c‘] [‘a‘, ‘b‘, ‘c‘, ‘d‘]
print(id(l1),id(l2),sep="/",end="\n")
# 31088848/31089608

3、最后在来看下深拷贝

l1 = ["a","b","c"]
l2 = copy.deepcopy(l1)
print(l1,l2)
# [‘a‘, ‘b‘, ‘c‘] [‘a‘, ‘b‘, ‘c‘]
print(id(l1),id(l2),sep="/",end="\n")
# 28450728/28467408

l2.append("d")
print(l1,l2)
# [‘a‘, ‘b‘, ‘c‘] [‘a‘, ‘b‘, ‘c‘, ‘d‘]
print(id(l1),id(l2),sep="/",end="\n")
# 28712872/28729552

结论:对于没有嵌套的list或者字典,如果使用赋值的方法,修改一个变量会对另外一个变量有影响,对于深拷贝和浅拷贝,修改一个变量,对另外一个变量是没有影响

然后我们在用有嵌套的字典来看下

1、

d1 = {"k1":"v1","k2":"v2","k3":["a","b","c"]}

d2 = d1
print(id(d1),id(d2))

# 27455632 27455632

d2["k1"] = "V1"
print(d2,d1)

# {‘k1‘: ‘V1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘a‘, ‘b‘, ‘c‘]} {‘k1‘: ‘V1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘a‘, ‘b‘, ‘c‘]}print(id(d1),id(d2))

# 27127952 27127952

d2["k3"][0] = "A"
print(d1,d2)
# {‘k1‘: ‘V1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘A‘, ‘b‘, ‘c‘]} {‘k1‘: ‘V1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘A‘, ‘b‘, ‘c‘]}

print(id(d1),id(d2))

# 26931344 26931344

 

2、在来看下浅拷贝

d1 = {"k1":"v1","k2":"v2","k3":["a","b","c"]}
d2 = copy.copy(d1)
print(id(d1),id(d2))
# 27455680 27831392

d2["k1"] = "V1"
print(d1,d2)
# {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘a‘, ‘b‘, ‘c‘]} {‘k1‘: ‘V1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘a‘, ‘b‘, ‘c‘]}
print(id(d1),id(d2))

# 7205056 7253088

d2["k3"][0] = "A"
print(d1,d2)
# {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘A‘, ‘b‘, ‘c‘]} {‘k1‘: ‘V1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘A‘, ‘b‘, ‘c‘]}
print(id(d1),id(d2))
# 26931392 29797472

 

3、最后在看下深拷贝

d1 = {"k1":"v1","k2":"v2","k3":["a","b","c"]}
d2 = copy.deepcopy(d1)
print(id(d1),id(d2))
# 27586704 30576880
print(d1,d2)
# {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘a‘, ‘b‘, ‘c‘]} {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘a‘, ‘b‘, ‘c‘]}

d2["k1"] = "V1"
print(d1,d2)
# {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘a‘, ‘b‘, ‘c‘]} {‘k1‘: ‘V1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘a‘, ‘b‘, ‘c‘]}
print(id(d1),id(d2))
# 27062416 28348656

d2["k3"][0] = "A"
print(id(d1),id(d2))
# 27193488 28152048
print(d1,d2)
# {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘a‘, ‘b‘, ‘c‘]} {‘k1‘: ‘V1‘, ‘k2‘: ‘v2‘, ‘k3‘: [‘A‘, ‘b‘, ‘c‘]}

  结论:对于赋值的方法,无论是修改第一层的值,还是第二层的值,均会对另外一个变量有影响;对于浅拷贝,修改第一层的值对另外一个变量没有影响,但是修改第二层的值,则会同步修改原来的变量;对于深拷贝,无论是修改第一层还是第二层甚至是更多层,都对原来的变量没有任何影响

 

以上是关于python之深浅拷贝的主要内容,如果未能解决你的问题,请参考以下文章

Python基础之小数据池及深浅拷贝

python之set集合深浅拷贝

Python之深浅拷贝

python之深浅拷贝

Python之深浅拷贝

Python之深浅拷贝