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: 未分类

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    code