代码1:

    >>> l1=range(3)
    >>> l2=l1
    >>> l2+=[4]
    >>> l1
    [0, 1, 2, 4]
    >>> l2
    [0, 1, 2, 4]

代码2:

>>> l2=l1
>>> l2=l2+[4]
>>> l1
[0, 1, 2]
>>> l2
[0, 1, 2, 4]

Why does += behave unexpectedly on lists?

本文固定链接: http://www.js-code.com/python/python_50196.html