简体   繁体   English

Python:为什么我的列表在我没有实际更改的情况下会更改?

[英]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.第二个语句“指向” mlist2mlist (即,它们都指向同一个列表对象),您对一个对象所做的任何更改都会反映在另一个中。

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之后, listlist2都指的是同一个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.

相关问题 Python - 如果我增加列表的大小,为什么我的 for 循环的长度 function 不会改变 - Python - Why doesn't the length function change for my for loop if I'm increasing the size of a list 在Python3中,当我读取二进制文件时,为什么&#39;b&#39;会放在内容之前? - In Python3, when I'm reading a binary file why does 'b' become prepended to my content? 为什么我更改复制的列表时原始列表会更改(PYTHON) - Why does the original list change when I change the copied list (PYTHON) 为什么我的代码认为我是在将列表与 int 进行比较? - Why does my code think I'm comparing a list to an int? 当我不按按钮时,为什么我的图像消失了? - Why does my image disappear when I'm not pressing a button? 当我将其添加到字典中的列表时,它实际上没有添加到列表中吗? 蟒蛇 - When i add to the list in my dictionary, it doesn't actually add to the list? python Python:为什么从对象检索列表后,列表会发生变化 - Python: why does my list change after I've retrieved it from an object Python 更改格式时更改列表的顺序 - Python changing order of a list when I change the formatting Python sorted()是否实际上会更改要排序的列表? - Does Python sorted() actually CHANGE the list being sorted? 为什么当我更改另一个列表时列表会发生变化? - Why does a list change when i change another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM