为什么 + 运算符不更改列表而 .append() 呢? [英] Why does not the + operator change a list while .append() does?

查看:45
本文介绍了为什么 + 运算符不更改列表而 .append() 呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Udacity 工作,Dave Evans 介绍了一个关于列表属性的练习

I'm working through Udacity and Dave Evans introduced an exercise about list properties

list1 = [1,2,3,4]
list2 = [1,2,3,4]

list1=list1+[6]
print(list1)
list2.append(6)
print(list2)

list1 = [1,2,3,4]
list2 = [1,2,3,4]

def proc(mylist):
    mylist = mylist + [6]

def proc2(mylist):
    mylist.append(6)

# Can you explain the results given by the four print statements below? Remove
# the hashes # and run the code to check.

print (list1)
proc(list1)
print (list1)

print (list2)
proc2(list2)
print (list2)

输出为

[1, 2, 3, 4, 6]
[1, 2, 3, 4, 6]
[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 4, 6]

那么在函数中,将 6 添加到集合中不会显示,但在不在函数中时会显示?

So in a function the adding a 6 to the set doesn't show but it does when not in a function?

推荐答案

那么在函数中,将 6 添加到集合中不会显示,但在不在函数中时会显示?

So in a function the adding a 6 to the set doesn't show but it does when not in a function?

不,事实并非如此.

结果是,当您执行 mylist = mylist + [6] 时,您实际上是在创建一个全新的列表并将其放入本地 mylist 变量中.这个mylist变量会在函数执行后消失,新创建的列表也会消失.

What happens is that, when you execute mylist = mylist + [6], you are effectively creating an entirely new list and putting it in the local mylist variable. This mylist variable will vanish after the execution of the function and the newly created list will vanish as well.

OTOH 当您执行 mylist.append(6) 时,您不会创建新列表.您可以在 mylist 变量中获得该列表,并将一个新元素添加到该列表中.结果是列表(也由 list2 指向)将被改变.mylist 变量将再次消失,但在这种情况下,您更改了原始列表.

OTOH when you execute mylist.append(6) you do not create a new list. You get the list already in the mylist variable and add a new element to this same list. The result is that the list (which is pointed by list2 too) will be altered itself. The mylist variable will vanish again, but in tis case you altered the original list.

让我们看看更直观的解释是否可以帮助您:)

Let us see if a more visual explanation can help you :)

当您编写 list1 = [1, 2, 3, 4, 5] 时,您正在创建一个新的列表对象(在等号的右侧)并创建一个新变量,list1,将指向这个对象.

When you write list1 = [1, 2, 3, 4, 5] you are creating a new list object (at the right side of the equals sign) and creating a new variable, list1, which will point to this object.

然后,当您调用 proc() 时,您会创建另一个新变量 mylist,并且由于您将 list1 作为参数传递,mylist 将指向同一个对象:

Then, when you call proc(), you create another new variable, mylist, and since you pass list1 as parameter, mylist will point to the same object:

然而,操作mylist + [6] 创建一个全新的列表对象,其内容是mylist指向的对象的内容加上以下列表对象的内容 - 即 [6].由于您将这个新对象归因于 mylist,我们的场景发生了一些变化,mylist 不再指向 list1 所指向的同一对象:

However, the operation mylist + [6] creates a whole new list object whose contents are the contents of the object pointed by mylist plus the content of the following list object - that is, [6]. Since you attribute this new object to mylist, our scenario changes a bit and mylist does not point to the same object pointed by list1 anymore:

我没有说的是mylist是一个局部变量:它会在proc()函数结束后消失.所以,当 proc() 执行结束时,mylist 消失了:

What I have not said is that mylist is a local variable: it will disappear after the end of the proc() function. So, when the proc() execution ended, the mylist is gone:

由于没有其他变量指向mylist + [6]生成的对象,它也会消失(因为垃圾收集器*会收集它):

Since no other variable points to the object generated by mylist + [6], it will disappear, too (since the garbage collector* will collect it):

注意,最后list1指向的对象并没有改变.

Note that, in the end, the object pointed by list1 is not changed.

当您调用 proc2() 时,一切都会改变.起初,它是一样的:你创建一个列表...

Everything changes when you call proc2(). At first, it is the same thing: you create a list...

...并将其作为参数传递给函数,该函数将生成一个局部变量:

...and pass it as a parameter to a function, which will generate a local variable:

但是,您不是使用 + 连接运算符(它生成新列表),而是将 append() 方法应用于现有列表.append() 方法不创建新对象;相反,它_改变了现有的:

However, instead of using the + concatenation operator, which generates a new list, you apply the append() method to the existing list. The append() method does not create a new object; instead, it _changes the existing one:

函数结束后,局部变量会消失,但是它和list1所指向的原始对象已经被改变了:

After the end of the function, the local variable will disappear, but the original object pointed by it and by list1 will be already altered:

由于它仍然被list1指向,所以原始列表没有被破坏.

Since it is still pointed by list1, the original list is not destroyed.

编辑:如果您想看看在您眼前发生的所有这些事情,只需访问这个非常惊人的模拟器:

EDIT: if you want to take a look at all this stuff happening before your eyes just go to this radically amazing simulator:

* 如果你不知道什么是垃圾收集器......好吧,你会在理解你自己的问题后很快发现.

* If you do not know what is garbage collector... well, you will discover soon after understanding your own question.

这篇关于为什么 + 运算符不更改列表而 .append() 呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆