S******y 发帖数: 1123 | 1 >>> x=[1,2,3]
>>> y=x
>>> x.remove(3)
>>> x
[1, 2]
>>> y
[1, 2]
As you can see, both x and y change same time. | c*******1 发帖数: 240 | 2 y = x
is called aliasing. x and y refer to the same list in memory.
you can type t
id(x)
id(y)
to see it.
x.remove
is a method of list, it modifies the list x points to
x = [1,2]
creates a new list and changes the address x is referring to
it is no different from
x = 'asdf'
or
x = 1.201
equal sign is an operation on the name x, not the object x points to.
just remember python has no variable. every so-called variable is a pointer | S******y 发帖数: 1123 | | s*********e 发帖数: 1051 | 4 >>> x = [1, 2, 3]
>>> y = []
>>> for i in x:
y.append(i)
>>> y
[1, 2, 3]
>>> x.remove(3)
>>> x
[1, 2]
>>> y
[1, 2, 3]
>>> |
|