在直接赋值、切片函数、浅拷贝、深拷贝上犯了太多次错,为了记住!
my_foods = ['a', 'b', 'c']
friends_food = my_food
my_foods.append('d')
friends_food.append('e')
friends_food = my_food[:]
my_foods.append('d')
friends_food.append('e')
一些深拷贝和浅拷贝的具体例子:
- 直接赋值:就是 a或者b中任何一个有任何一点改动,就会你动我也动;
- 切片赋值和浅拷贝:嵌套的会互相影响,如果是独立的一个,即完全不受影响,独立的可以和深拷贝一样考虑,嵌套的部分和直接赋值一样考虑;
- 深拷贝:怎么样都互相不影响了;
import copy
a = ['world', [1,2,3,4]]
b = a.copy()
print(b)
print(id(b) == id(a))
print("===========")
b[1].append(5)
print(a)
print(b)
print(a == b)
print("****************")
a[1].append(6)
print(a)
print(b)
print(a == b)
print("----------------------")
a[0] = 'hello'
print(a)
print(b)
print(a == b)
b[0] = 'xixi'
print(a)
print(b)
print(a == b)
print("+++++++++++++++++++++++")
a[1].remove(6)
print(a)
print(b)
print(a == b)
a.remove('hello')
print(a)
print(b)
print(a == b)
"""
结果如下:
['world', [1, 2, 3, 4]]
False
===========
['world', [1, 2, 3, 4, 5]]
['world', [1, 2, 3, 4, 5]]
True
****************
['world', [1, 2, 3, 4, 5, 6]]
['world', [1, 2, 3, 4, 5, 6]]
True
----------------------
['hello', [1, 2, 3, 4, 5, 6]]
['world', [1, 2, 3, 4, 5, 6]]
False
['hello', [1, 2, 3, 4, 5, 6]]
['xixi', [1, 2, 3, 4, 5, 6]]
False
+++++++++++++++++++++++
['hello', [1, 2, 3, 4, 5]]
['xixi', [1, 2, 3, 4, 5]]
False
[[1, 2, 3, 4, 5]]
['xixi', [1, 2, 3, 4, 5]]
False
"""
import copy
a = ['world', [1,2,3,4]]
b = a.copy()
print(b)
print(id(b) == id(a))
print("===========")
b[1].append(5)
print(a)
print(b)
print(a == b)
print("****************")
a[1].append(6)
print(a)
print(b)
print(a == b)
print("----------------------")
a[0] = 'hello'
print(a)
print(b)
print(a == b)
b[0] = 'xixi'
print(a)
print(b)
print(a == b)
print("+++++++++++++++++++++++")
a[1].remove(6)
print(a)
print(b)
print(a == b)
a.remove('hello')
print(a)
print(b)
print(a == b)
"""
结果如下:
['world', [1, 2, 3, 4]]
False
===========
['world', [1, 2, 3, 4, 5]]
['world', [1, 2, 3, 4, 5]]
True
****************
['world', [1, 2, 3, 4, 5, 6]]
['world', [1, 2, 3, 4, 5, 6]]
True
----------------------
['hello', [1, 2, 3, 4, 5, 6]]
['world', [1, 2, 3, 4, 5, 6]]
False
['hello', [1, 2, 3, 4, 5, 6]]
['xixi', [1, 2, 3, 4, 5, 6]]
False
+++++++++++++++++++++++
['hello', [1, 2, 3, 4, 5]]
['xixi', [1, 2, 3, 4, 5]]
False
[[1, 2, 3, 4, 5]]
['xixi', [1, 2, 3, 4, 5]]
False
"""
import copy
a = ['world', [1,2,3,4]]
b = copy.deepcopy(a)
print(b)
print(id(b) == id(a))
print("===========")
b[1].append(5)
print(a)
print(b)
print(a == b)
print("****************")
a[1].append(6)
print(a)
print(b)
print(a == b)
print("----------------------")
a[0] = 'hello'
print(a)
print(b)
print(a == b)
b[0] = 'xixi'
print(a)
print(b)
print(a == b)
print("+++++++++++++++++++++++")
a[1].remove(6)
print(a)
print(b)
print(a == b)
a.remove('hello')
print(a)
print(b)
print(a == b)
"""
结果如下:
['world', [1, 2, 3, 4]]
False
===========
['world', [1, 2, 3, 4]]
['world', [1, 2, 3, 4, 5]]
False
****************
['world', [1, 2, 3, 4, 6]]
['world', [1, 2, 3, 4, 5]]
False
----------------------
['hello', [1, 2, 3, 4, 6]]
['world', [1, 2, 3, 4, 5]]
False
['hello', [1, 2, 3, 4, 6]]
['xixi', [1, 2, 3, 4, 5]]
False
+++++++++++++++++++++++
['hello', [1, 2, 3, 4]]
['xixi', [1, 2, 3, 4, 5]]
False
[[1, 2, 3, 4]]
['xixi', [1, 2, 3, 4, 5]]
False
"""