The GIST to take is this:
Dealing with shallow lists (no sub_lists, just single elements) using "normal assignment" rises a "side effect" when you create a shallow list and then you create a copy of this list using "normal assignment". This "side effect" is when you change any element of the copy list created, because it will automatically change the same elements of the original list. That is when copy
comes in handy, as it won t change the original list elements when changing the copy elements.
On the other hand, copy
does have a "side effect" as well, when you have a list that has lists in it (sub_lists), and deepcopy
solves it. For instance if you create a big list that has nested lists in it (sub_lists), and you create a copy of this big list (the original list). The "side effect" would arise when you modify the sub_lists of the copy list which would automatically modify the sub_lists of the big list. Sometimes (in some projects) you want to keep the big list (your original list) as it is without modification, and all you want is to make a copy of its elements (sub_lists). For that, your solution is to use deepcopy
which will take care of this "side effect" and makes a copy without modifying the original content.
<代码>copy和deep份
业务仅涉及复合物体(即:含有其他物体的物体,如清单)。
下面是这一简单法典例子所显示的差别:
<><>>>>>>>>
请通过创建原始名单和这份名单的复印件,检查<代码>copy(鲸目):
import copy
original_list = [1, 2, 3, 4, 5, [ a , b ]]
copy_list = copy.copy(original_list)
现在,请进行一些<代码>的印本/编码>测试,并了解原始清单与其复印清单的对比:
原件_清单和副本-清单有不同的地址
print(hex(id(original_list)), hex(id(copy_list))) # 0x1fb3030 0x1fb3328
原件——清单和复印件——清单内容相同
print(hex(id(original_list[1])), hex(id(copy_list[1]))) # 0x537ed440 0x537ed440
sub_原件——清单和复印件——清单内容相同
print(hex(id(original_list[5])), hex(id(copy_list[5]))) # 0x1faef08 0x1faef08
更改原清单内容的NOT修改了正本-清单内容
original_list.append(6)
print("original_list is:", original_list) # original_list is: [1, 2, 3, 4, 5, [ a , b ], 6]
print("copy_list is:", copy_list) # copy_list is: [1, 2, 3, 4, 5, [ a , b ]]
更改正本-清单内容,NOT修改原清单内容
copy_list.append(7)
print("original_list is:", original_list) # original_list is: [1, 2, 3, 4, 5, [ a , b ], 6]
print("copy_list is:", copy_list) # copy_list is: [1, 2, 3, 4, 5, [ a , b ], 7]
修改原有的“名单”子_elements
original_list[5].append( c )
print("original_list is:", original_list) # original_list is: [1, 2, 3, 4, 5, [ a , b , c ], 6]
print("copy_list is:", copy_list) # copy_list is: [1, 2, 3, 4, 5, [ a , b , c ], 7]
modifying copy_list sub_elements automatically modify original_list sub_elements
copy_list[5].append( d )
print("original_list is:", original_list) # original_list is: [1, 2, 3, 4, 5, [ a , b , c , d ], 6]
print("copy_list is:", copy_list) # copy_list is: [1, 2, 3, 4, 5, [ a , b , c , d ], 7]
<><>>>><>>>>>>>><>>>>>>
让我们检查一下<条码>处理范围广的条码>,与我们在<条码>上所做的那样做。 (填写一份清单和一份清单):
import copy
original_list = [1, 2, 3, 4, 5, [ a , b ]]
copy_list = copy.copy(original_list)
现在,请进行一些<代码>的印本/编码>测试,并了解原始清单与其复印清单的对比:
import copy
original_list = [1, 2, 3, 4, 5, [ a , b ]]
copy_list = copy.deepcopy(original_list)
原件_清单和副本-清单有不同的地址
print(hex(id(original_list)), hex(id(copy_list))) # 0x1fb3030 0x1fb3328
原件——清单和复印件——清单内容相同
print(hex(id(original_list[1])), hex(id(copy_list[1]))) # 0x537ed440 0x537ed440
sub_elements of 原件_清单和副本-清单有不同的地址
print(hex(id(original_list[5])), hex(id(copy_list[5]))) # 0x24eef08 0x24f3300
更改原清单内容的NOT修改了正本-清单内容
original_list.append(6)
print("original_list is:", original_list) # original_list is: [1, 2, 3, 4, 5, [ a , b ], 6]
print("copy_list is:", copy_list) # copy_list is: [1, 2, 3, 4, 5, [ a , b ]]
更改正本-清单内容,NOT修改原清单内容
copy_list.append(7)
print("original_list is:", original_list) # original_list is: [1, 2, 3, 4, 5, [ a , b ], 6]
print("copy_list is:", copy_list) # copy_list is: [1, 2, 3, 4, 5, [ a , b ], 7]
NOT修改了原“名单子”
original_list[5].append( c )
print("original_list is:", original_list) # original_list is: [1, 2, 3, 4, 5, [ a , b , c ], 6]
print("copy_list is:", copy_list) # copy_list is: [1, 2, 3, 4, 5, [ a , b ], 7]
更改名单
copy_list[5].append( d )
print("original_list is:", original_list) # original_list is: [1, 2, 3, 4, 5, [ a , b , c , d ], 6]
print("copy_list is:", copy_list) # copy_list is: [1, 2, 3, 4, 5, [ a , b , d ], 7]