[英]Python: why does my list change when I'm not actually changing it?
Newbie with a question, so please be gentle:菜鸟有问题,请客气点:
list = [1, 2, 3, 4, 5]
list2 = list
def fxn(list,list2):
for number in list:
print(number)
print(list)
list2.remove(number)
print("after remove list is ", list, " and list 2 is ", list2)
return list, list2
list, list2 = fxn(list, list2)
print("after fxn list is ", list)
print("after fxn list2 is ", list2)
This results in:这导致:
1
[1, 2, 3, 4, 5]
after remove list is [2, 3, 4, 5] and list 2 is [2, 3, 4, 5]
3
[2, 3, 4, 5]
after remove list is [2, 4, 5] and list 2 is [2, 4, 5]
5
[2, 4, 5]
after remove list is [2, 4] and list 2 is [2, 4]
after fxn list is [2, 4]
after fxn list2 is [2, 4]
I don't understand why list is changing when I am only doing list2.remove()
, not list.remove()
.我不明白为什么当我只做
list2.remove()
而不是list.remove()
时 list 会改变。 I'm not even sure what search terms to use to figure it out.我什至不确定使用什么搜索词来弄清楚。
The reason this is happening can be found here:发生这种情况的原因可以在这里找到:
mlist = [1,2,3,4,5]
mlist2 = mlist
the second statement "points" mlist2
to mlist
(ie, they both refer to the same list object) and any changes you make to one is reflected in the other.第二个语句“指向”
mlist2
到mlist
(即,它们都指向同一个列表对象),您对一个对象所做的任何更改都会反映在另一个中。
To make a copy instead try this (using a slice operation):要制作副本,请尝试此操作(使用切片操作):
mlist = [1,2,3,4,5]
mlist2 = mlist[:]
In case you are curious about slice notation, this SO question Python Lists(Slice method) will give you more background.如果您对切片表示法感到好奇,这个问题Python Lists(Slice method)将为您提供更多背景知识。
Finally , it is not a good idea to use list
as an identifier as Python already uses this identifier for its own data structure (which is the reason I added the " m
" in front of the variable names)最后,使用
list
作为标识符并不是一个好主意,因为 Python 已经将这个标识符用于它自己的数据结构(这就是我在变量名前添加“ m
”的原因)
That's because both list
and list2
are referring to the same list after you did the assignment list2=list
.这是因为在您完成分配
list2=list
之后, list
和list2
都指的是同一个list2=list
。
Try this to see if they are referring to the same objects or different:试试这个,看看它们是指相同的对象还是不同的:
id(list)
id(list2)
An example:一个例子:
>>> list = [1, 2, 3, 4, 5]
>>> list2 = list
>>> id(list)
140496700844944
>>> id(list2)
140496700844944
>>> list.remove(3)
>>> list
[1, 2, 4, 5]
>>> list2
[1, 2, 4, 5]
If you really want to create a duplicate copy of list
such that list2
doesn't refer to the original list but a copy of the list, use the slice operator:如果您真的想创建
list
副本,以便list2
不引用原始列表而是引用列表的副本,请使用切片运算符:
list2 = list[:]
An example:一个例子:
>>> list
[1, 2, 4, 5]
>>> list2
[1, 2, 4, 5]
>>> list = [1, 2, 3, 4, 5]
>>> list2 = list[:]
>>> id(list)
140496701034792
>>> id(list2)
140496701034864
>>> list.remove(3)
>>> list
[1, 2, 4, 5]
>>> list2
[1, 2, 3, 4, 5]
Also, don't use list
as a variable name, because originally, list
refers to the type list, but by defining your own list
variable, you are hiding the original list
that refers to the type list.另外,不要使用
list
作为变量名,因为本来, list
指式列表中,但定义自己的list
变量,你是隐藏原来的list
是指类型列表。 Example:例子:
>>> list
<type 'list'>
>>> type(list)
<type 'type'>
>>> list = [1, 2, 3, 4, 5]
>>> list
[1, 2, 3, 4, 5]
>>> type(list)
<type 'list'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.