为什么是这样? [英] Why is this?

查看:52
本文介绍了为什么是这样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有初始化问题。

Hi everyone,

I have a problem with initialization.

a ,b = [[]] * 2
a.append(1)
b
[1]


为什么会这样?为什么这不像下面那样:

a,b = [[],[]]
a.append(1)
b
[]


而且,只是为了增加我的困惑:

[[]] * 2
[[],[]] [[],[] ] == [[]] * 2
a, b = [[]]*2
a.append(1)
b [1]

Why is this? Why does not this behave like the below:
a, b = [[], []]
a.append(1)
b []

And, just to add to my confusion:
[[]]*2 [[], []] [[], []] == [[]]*2



True


提前感谢您的解释。

jbar

BTW,如果重要的话......

Python 2.4.1(#2,2005年3月30日,20:41:35)

[GCC 3.3.5(Debian 1:3.3.5-8ubuntu2)] on linux2


True

Thanks in advance for the explanation.
jbar
BTW, if it matters...
Python 2.4.1 (#2, Mar 30 2005, 20:41:35)
[GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2

推荐答案

Jiri Barton写道:
Jiri Barton wrote:
大家好,

我的初始化有问题。
Hi everyone,

I have a problem with initialization.
a,b = [[]] * 2
a.append(1)
b [1]

为什么会这样?为什么这不像下面那样:
a,b = [[]] * 2
a == b
a, b = [[]]*2
a.append(1)
b [1]

Why is this? Why does not this behave like the below:
a, b = [[]]*2
a==b


True

True


而且,只是为了增加我的困惑:

[[]] * 2
[[],[]]
[[],[]] == [ []] * 2
And, just to add to my confusion:

[[]]*2
[[], []]
[[], []] == [[]]*2



True



True



这也让我感到困惑,看起来空列表共享同一个对象。


Paolino



___________________________________

Yahoo!邮件:免费1GB每日消息10MB
http://mail.yahoo.it


嗨吉日,


2005年8月10日星期三11:40:54 +0100,Jiri Barton < JB ** @ hosting4u.cz>写道:
Hi Jiri,

On Wed, 10 Aug 2005 11:40:54 +0100, Jiri Barton <jb**@hosting4u.cz> wrote:
我的初始化有问题。
a,b = [[]] * 2 a.append(1)
b [1]

为什么会这样?为什么这不像下面那样:
a,b = [[],[]]
a.append(1)
b []
a, b = [[]]*2
a.append(1)
b [1]

Why is this? Why does not this behave like the below:
a, b = [[], []]
a.append(1)
b []




在第一种情况下,您创建一个包含单个空列表的列表。

" * 2"操作复制该列表中的项目。所以现在a和b

包含相同的空列表实例。而在第二种情况下,

你明确声明了一个空列表的两个独立实例。


append方法修改现有的列表对象 - 它改变了值

它本身就存在。因此,在第一个例子中,因为a和b

都指向同一个对象,改变''a''也会出现

更改''b' 。第二个例子也是如此,因为它们是不同的对象。


一个更简单的例子:



In the first case, you make a list containing a single empty list. The
"*2" operation duplicates the item in that list. So now both a and b
contain the same instance of an empty list. Whereas in the second case,
you explicity declare two separate instances of an empty list.

The append method modifys the existing list object - it changes the values
it holds inside itself. Therefore, in the first example, because a and b
both refer to the same object, changing ''a'' will will appear to also
change ''b''. The same is not true of the second example, because they are
different objects.

A simpler example:

a = []
b = []
c = b


现在让我们尝试一些测试:

a == b
真b == c



好​​的 - 所以这表明a和b,以及b和c相等_

他们相同的价值_。但这并不意味着它们是相同的

实际对象:

a是b
假b是c



b和c是相同的空列表对象。但是a和b是分开的,

不同的空列表对象。


为什么区别?想象一下,你制作了一组对象来代表一些

类的数值,例如。向量。询问A == B 是是矢量A的值,它是矢量A的值,与矢量B的值相同。但是要求A是B

表示对象A是与B相同的对象。

[[]] * 2 [[],[] ] [[],[]] == [[]] * 2 True
a = []
b = []
c = b
Now lets try some tests:
a == b True b == c True

Ok - so this shows that a and b, and b and c are equal _in the sense that
they amount to the same value_. But that doesn''t mean they are the same
actual object:
a is b False b is c True

b and c are the same empty list object. But a and b are separate,
different, empty list objects.

Why the difference? Imagine you made a set of objects to represent some
kind of numeric values, eg. vectors. Asking "A==B" means "is the value of
the vector A, the same as the value of the vector B". But asking "A is B"
means "is the object A the same object as B".
[[]]*2 [[], []] [[], []] == [[]]*2 True




同样效果。但是试试''是''运算符,看看它们是否实际上是''空列表''的相同实例:



Same effect. But try the ''is'' operator, to see if they are actually the
same instances of ''empty list'':

[[] ,[]]是[[]] * 2
[[], []] is [[]]*2


True


True




希望这会有所帮助......我的第一次去在解释这种东西的时候,所以如果它不是那么清楚就会道歉!


马特

-


|马特哈蒙德

|英国萨里Tadworth BBC研发部R& D工程师。



Hope this helps ... its my first go at explaining this kind of stuff, so
apologies if it isn''t as clear as it could be!

Matt

--

| Matt Hammond
| R&D Engineer, BBC Research and Development, Tadworth, Surrey, UK.


Paolino写道:
Paolino wrote:
Jiri Barton写道:
Jiri Barton wrote:
大家好,

我的初始化有问题。

Hi everyone,

I have a problem with initialization.

> a,b = [[ ]] * 2
> a.append(1)
> b
[1]

为什么会这样?为什么这不像下面那样:
>a, b = [[]]*2
>a.append(1)
>b
[1]

Why is this? Why does not this behave like the below:


>>> a,b = [[]] * 2
>>> a == b
>>> a, b = [[]]*2
>>> a==b


True


True



哎呀我应该写''a is b''


Ooops I should write ''a is b''

而且,只是为了增加我的困惑:
And, just to add to my confusion:
> [[]] * 2
[[],[]]

>[[]]*2
[[], []]

> [[],[]] == [[]] * 2
>[[], []] == [[]]*2



True



True



这也让我感到困惑,看起来像空列表共享相同的对象。



This confuses me also,looks like empty lists share same object.



这实际上可以正常比较列表的内容;-)

Paolino


___________________________________
雅虎!邮件:免费1GB每日消息10MB
http://mail.yahoo.it



___________________________________

Aggiungi la toolbar di Yahoo!搜索sul tuo浏览器,e''gratis!
http://it.toolbar。 yahoo.com


这篇关于为什么是这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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