python list 复制改变原list list赋值操作问题

if you use list of Python3, you can not assign new variable  a value using "=" directly. For, if the child variable was changed, original variable would be changed.

the operation of copy should be used.

it was tested as following:

>>> a=[1,2,3]
>>> b=a
>>> b
[1, 2, 3]
>>> b[0]=8
>>> a
[8, 2, 3]
>>> b
[8, 2, 3]

And if used copy, it would not affect original variable.

>>> import copy as cp
>>> c=cp.copy(a)
>>> c[0]=100
>>> c
[100, 2, 3]
>>> a
[8, 2, 3]
>>> 

Lastly, i recommand deepcopy, for deepcopy is most clear, it would affect anything of the original one, it set a wholely new object.

posted @ 2020-07-30 09:56  信雪神话  阅读(423)  评论(0编辑  收藏  举报