Python之深浅拷贝
浅copy:
1.是把列表本身独立,里面的元素是被两个列表共享的
2.当其中一个列表中的元素被改变时,另一个列表中的元素不会随之变化。
3.当列表里面还有列表时,修改内部列表的内容时,另一个列表中的内部列表也会随之变化
a1=["a","b","c","aa"]
b1=a1
a1[0]="1"
print(a1,b1)
此时结果为:
a1=["1","b","c","aa"]
b1=["1","b","c","aa"]
深copy:
import copy
列表=copy.deepcopy(列表)
当用到深copy时,两个列表的所有内容都是独立的,没有任何关系。
a1=["a","b","c","aa"]
c1=a1.copy()
a1[0]="1"
print(a1,c1)
此时结果为:
a1=["1","b","c","aa"]
b1=["a","b","c","aa"]