Python copy() function explained (list,array)
list
The python variable name is equivalent to the tag name list2 = list1, a direct assignment that essentially points to the same memory value.
A change in either variable (list1 or list2) affects the other.
.
>>> list1=[1,2,3,4,5,6] >>> list2=list1 >>> list1[2]=88 >>> list1 [1, 2, 88, 4, 5, 6] >>> list2 [1, 2, 88, 4, 5, 6]
And list3 and list4 are copy operations of list1 by slicing, each pointing to a new value.
Arbitrarily changing the value of list3 or list4 will not affect the others.
array
There is a slight difference between the array type and the list type when copying data.
An array slice is a view of the original array, which means that the data is not copied and any modifications on the view are reflected directly on the source array.
array1, array2, array3, array4 actually point to the same memory value, and any modification to one of these variables will modify the values of the others.
If you want to get a copy of the array slice instead of a view, you need to explicitly perform the copy operation function copy().
.
array5 = () # A copy of the original array1 array6 = array[1:4].copy() # slide prepared for microscopyarray[1:4]replication
Then, modifying array5 or array6 will not affect array1
Python dictionary copy function
Functions of copy
Make a new copy of the current dictionary (not the same memory address as the original)
Usage of copy
Usage.
() ->This function takes no arguments,Returns an identical dictionary with different memory addresses In [33]: old_dict = {'name' : 'insane' , 'age' : 33} In [34]: new_dict = ld_dict.copy() In [35]: id(new_dict) != id(old_dict) Out[35]: True
real combat
# coding:utf-8 fruits = { 'apple': 30, 'banana': 50, 'pear': 100 } real_fruits = () print(real_fruits) real_fruits['orange'] = 50 real_fruits.update({'cherry': 100}) print(real_fruits) print('Original fruits:', fruits) real_fruits['apple'] = real_fruits['apple'] - 5 print(real_fruits) real_fruits['pear'] -= 3 print(real_fruits) real_fruits.clear() print(real_fruits) print('The next day...') real_fruits = () print(real_fruits)
{'apple': 30, 'banana': 50, 'pear': 100} {'apple': 30, 'banana': 50, 'pear': 100, 'orange': 50, 'cherry': 100} original (document etc)fruits: {'apple': 30, 'banana': 50, 'pear': 100} {'apple': 25, 'banana': 50, 'pear': 100, 'orange': 50, 'cherry': 100} {'apple': 25, 'banana': 50, 'pear': 97, 'orange': 50, 'cherry': 100} {} the morrow。。。 {'apple': 30, 'banana': 50, 'pear': 100} Process finished with exit code 0
summarize
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.