标签:深浅拷贝 apple size 浅拷贝 epc 内容 对象 strong print
‘‘‘
列表赋值:不会创建新对象
两个列表的内存地址一样
‘‘‘
# lst1 = ["apple", "banana", ["blueberry", "strawberry"]]
# lst2 = lst1
# print(lst1)
# print(lst2)
# print(id(lst1))
# print(id(lst2))
# lst1.append("watermelon")
# print(lst1)
# print(lst2)
# lst1.append("pear")
# print(lst1)
# print(lst2)
‘‘‘
拷贝的意义:快速的创建对象
浅拷贝:只拷贝第一层内容,复制一个表面,更深层次的内容,只拷贝一个内存地址
1.列表切片赋值
两个列表的内存地址不一样
‘‘‘
# lst1 = ["apple", "banana", ["blueberry", "strawberry"]]
# lst2 = lst1[:]
# print(lst1)
# print(lst2)
# print(id(lst1))
# print(id(lst2))
# lst1.append("watermelon")
# print(lst1)
# print(lst2)
# lst1[2].append("pear")
# print(lst1)
# print(lst2)
‘‘‘
拷贝的意义:快速的创建对象
浅拷贝:只拷贝第一层内容,复制一个表面,更深层次的内容,只拷贝一个内存地址
2.copy()
两个列表的内存地址不一样
‘‘‘
# lst1 = ["apple", "banana", ["blueberry", "strawberry"]]
# lst2 = lst1.copy()
# print(lst1)
# print(lst2)
# print(id(lst1))
# print(id(lst2))
# lst1.append("watermelon")
# print(lst1)
# print(lst2)
# lst1[2].append("pear")
# print(lst1)
# print(lst2)
‘‘‘
深拷贝:把内容完全的复制一份
import copy
copy.deepcopy()
两个列表的内存地址不一样,两个列表没有任何的关联了
‘‘‘
# import copy
# lst1 = ["apple", "banana", ["blueberry", "strawberry"]]
# lst2 = copy.deepcopy(lst1)
# print(lst1)
# print(lst2)
# print(id(lst1))
# print(id(lst2))
# lst1.append("watermelon")
# print(lst1)
# print(lst2)
# lst1[2].append("pear")
# print(lst1)
# print(lst2)
标签:深浅拷贝 apple size 浅拷贝 epc 内容 对象 strong print
原文地址:https://www.cnblogs.com/lilyxiaoyy/p/10716274.html