为什么不+运算符变化的名单,而.append()呢? [英] Why does not the + operator change a list while .append() does?

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

问题描述

我通过Udacity和戴夫·埃文斯的工作介绍了列表属性的练习

  list1的= [1,2,3,4]
列表2 = [1,2,3,4]list1的= list1的+ [6]
打印(list1的)
list2.append(6)
打印(列表2)list1的= [1,2,3,4]
列表2 = [1,2,3,4]高清PROC(MYLIST):
    MYLIST = MYLIST + [6]高清PROC2(MYLIST):
    mylist.append(6)#你能解释一下由下四个打印报表给出的结果?去掉
#散列#和运行code检查。打印(list1的)
PROC(list1的)
打印(list1的)打印(列表2)
PROC2(列表2)
打印(列表2)

的输出是

  [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至不显示,但它不用时功能?


解决方案

  

因此​​,在一个函数添加一组6至不显示,但它不用时功能?


没有,这是不是会发生什么。

会发生什么事是,当你执行 MYLIST = MYLIST + [6] 时,实际上是创建一个全新的列表,并把它在当地的 MYLIST 变量。该函数的执行和新创建的列表将消失,以及在此之后 MYLIST 变量将消失。

OTOH当你执行 mylist.append(6)不创建一个新的列表。你得到的名单已经在 MYLIST 变量,并添加新元素此相同的列表。其结果是,该列表(这是由 list2中指出太)将自身发生改变。在 MYLIST 变量会再次消失,但在TIS如果你改变了原来的列表中。

让我们看看一个更直观的解释可以帮助你:)

当你调用会发生什么 PROC()

当你写 list1的= [1,2,3,4,5] 要创建一个新的列表对象(在等号的右边号)和创建一个新的变量, list1的,这将指向这个对象。

然后,当你调用 PROC(),再创建一个新的变量, MYLIST ,既然你传递 list1的作为参数, MYLIST 将指向同一个对象:

但是,操作 MYLIST + [6] 创建了一个全新的列表对象的,其内容是由<$指向的对象的内容C $ C> MYLIST 加上下面的列表对象的内容 - 也就是 [6] 。既然你认为这新的对象为 MYLIST ,我们的方案更改了一下, MYLIST 不指向同一个对象指向通过 list1的了:

我没有说的是, MYLIST 局部变量的:的结束后它会消失 PROC()功能。所以,当 PROC()执行结束后, MYLIST 消失:

由于没有其他变量指向由 MYLIST +生成的目标[6] ,它会消失,太多(因为垃圾收集器*将收集它):

需要注意的是,在年底,目的是通过 list1的指出不会改变。

当你调用会发生什么 PROC2()

当你调用一切都在变化 PROC2()。首先,它是同一件事:创建一个列表...

...和它作为参数传递给函数,这将产生一个局部变量:

然而,而是采用了 + 连接符,产生一个新的列表,你申请了追加()方法将现有列表。在追加()方法的不创建一个新的对象的;相反,它_changes现有之一:

在该函数结束时,局部变量会消失,但通过将其与由 list1的指出原始对象将已改变的:

由于它仍然是由 list1的指出,原来的名单不被破坏。

修改:如果你想看看所有这些事情发生的在您的眼前只是去<一个href=\"http://www.pythontutor.com/visualize.html#$c$c=list1+=+%5B1,2,3,4%5D%0Alist2+=+%5B1,2,3,4%5D%0A%0Adef+proc%28mylist%29%3a%0A++++mylist+=+mylist+++%5B6%5D%0A%0Adef+proc2%28mylist%29%3a%0A++++mylist.append%286%29%0A%0A#+Can+you+explain+the+results+given+by+the+four+print+statements+below?%2bRemove%0A#%2bthe%2bhashes%2b#%2band%2brun%2bthe%2b$c$c%2bto%2bcheck.%0A%0Aprint%2b%28list1%29%0Aproc%28list1%29%0Aprint%2b%28list1%29%0A%0Aprint%2b%28list2%29%0Aproc2%28list2%29%0Aprint%2b%28list2%29&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=2&rawInputLstJSON=%5B%5D&curInstr=16\"相对=nofollow>这从根本上惊人的模拟器:

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

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)

The output is

[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]

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

解决方案

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

No, that is not what happens.

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 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 :)

What happens when you call proc()

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.

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:

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:

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:

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

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

What happens when you call 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:

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:

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:

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天全站免登陆