copy problem in Python

No Comments

Look at codes below:

>>> v = [0.5, 0.75, 1.0, 1.5, 2.0]
>>> m = [v, v, v]
>>> v[0] = ‘Python’
>>> m
[[‘Python’, 0.75, 1.0, 1.5, 2.0], [‘Python’, 0.75, 1.0, 1.5, 2.0], [‘Python’, 0.75, 1.0, 1.5, 2.0]]
>>> from copy import deepcopy
>>> v = [0.5, 0.75, 1.0, 1.5, 2.0]
>>> m = 3*[deepcopy(v), ]
>>> v[0] = ‘Python’
>>> m
[[0.5, 0.75, 1.0, 1.5, 2.0], [0.5, 0.75, 1.0, 1.5, 2.0], [0.5, 0.75, 1.0, 1.5, 2.0]]

Categories: 未分类

发表评论

邮箱地址不会被公开。 必填项已用*标注

*

code