Python将多个变量分配给相同的值?列出行为 [英] Python assigning multiple variables to same value? list behavior

查看:102
本文介绍了Python将多个变量分配给相同的值?列出行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用如下所示的多重赋值来初始化变量,但是我对此行为感到困惑,我希望分别重新赋值列表,我的意思是b [0]和c [0]等于0. /p>

I tried to use multiple assignment as show below to initialize variables, but I got confused by the behavior, I expect to reassign the values list separately, I mean b[0] and c[0] equal 0 as before.

a=b=c=[0,3,5]
a[0]=1
print(a)
print(b)
print(c)

结果是: [1、3、5] [1、3、5] [1、3、5]

Result is: [1, 3, 5] [1, 3, 5] [1, 3, 5]

那是正确的吗?多重分配应该使用什么? 与此有何不同?

Is that correct? what should I use for multiple assignment? what is different from this?

d=e=f=3
e=4
print('f:',f)
print('e:',e)

结果: ('f:',3) ('e:',4)

result: ('f:', 3) ('e:', 4)

推荐答案

如果您要使用C/Java/etc中的一种语言来学习Python.家族,它可能会帮助您停止将a视为变量",而开始将其视为名称".

If you're coming to Python from a language in the C/Java/etc. family, it may help you to stop thinking about a as a "variable", and start thinking of it as a "name".

abc并不是具有相等值的不同变量;它们是相同名称的不同名称.变量具有类型,标识,地址以及类似的各种内容.

a, b, and c aren't different variables with equal values; they're different names for the same identical value. Variables have types, identities, addresses, and all kinds of stuff like that.

名称中没有任何名称. 当然可以,并且对于相同的值,您可以有很多名称.

Names don't have any of that. Values do, of course, and you can have lots of names for the same value.

如果给Notorious B.I.G.热狗,* Biggie SmallsChris Wallace都有热狗.如果将a的第一个元素更改为1,则bc的前一个元素为1.

If you give Notorious B.I.G. a hot dog,* Biggie Smalls and Chris Wallace have a hot dog. If you change the first element of a to 1, the first elements of b and c are 1.

如果您想知道两个名称是否在命名同一对象,请使用is运算符:

If you want to know if two names are naming the same object, use the is operator:

>>> a=b=c=[0,3,5]
>>> a is b
True


然后您问:


You then ask:

与此有何不同?

what is different from this?

d=e=f=3
e=4
print('f:',f)
print('e:',e)

在这里,您要将名称e重新绑定到值4.这不会以任何方式影响名称df.

Here, you're rebinding the name e to the value 4. That doesn't affect the names d and f in any way.

在以前的版本中,您是分配给a[0]而不是a.因此,从a[0]的角度来看,您正在重新绑定a[0],但是从a的角度来看,您正在就地更改它.

In your previous version, you were assigning to a[0], not to a. So, from the point of view of a[0], you're rebinding a[0], but from the point of view of a, you're changing it in-place.

您可以使用id函数,该函数为您提供代表对象身份的唯一编号,以准确查看哪个对象是哪一个对象,即使is无法解决问题:

You can use the id function, which gives you some unique number representing the identity of an object, to see exactly which object is which even when is can't help:

>>> a=b=c=[0,3,5]
>>> id(a)
4473392520
>>> id(b)
4473392520
>>> id(a[0])
4297261120
>>> id(b[0])
4297261120

>>> a[0] = 1
>>> id(a)
4473392520
>>> id(b)
4473392520
>>> id(a[0])
4297261216
>>> id(b[0])
4297261216

请注意,a[0]已从4297261120更改为4297261216-现在它是一个具有不同值的名称.现在,b[0]也是该相同新值的名称.这是因为ab仍在命名同一对象.

Notice that a[0] has changed from 4297261120 to 4297261216—it's now a name for a different value. And b[0] is also now a name for that same new value. That's because a and b are still naming the same object.

在幕后,a[0]=1实际上是在列表对象上调用方法. (等效于a.__setitem__(0, 1).)因此,它根本不是真正重新绑定任何东西.这就像调用my_object.set_something(1).当然,该对象可能是重新绑定实例属性以实现此方法,但这并不重要.重要的是您没有分配任何东西,只是对对象进行了变异.和a[0]=1一样.

Under the covers, a[0]=1 is actually calling a method on the list object. (It's equivalent to a.__setitem__(0, 1).) So, it's not really rebinding anything at all. It's like calling my_object.set_something(1). Sure, likely the object is rebinding an instance attribute in order to implement this method, but that's not what's important; what's important is that you're not assigning anything, you're just mutating the object. And it's the same with a[0]=1.

user570826问:

user570826 asked:

如果有的话,a = b = c = 10

a = b = c = [1, 2, 3]的情况完全相同:您有三个具有相同值的名称.

That's exactly the same situation as a = b = c = [1, 2, 3]: you have three names for the same value.

但是在这种情况下,值是int,而int是不可变的.无论哪种情况,都可以将a重新绑定到其他值(例如a = "Now I'm a string!"),但不会影响原始值,而bc仍将是原始值.区别在于,通过列表,您可以通过执行例如a.append(4)来将值[1, 2, 3]更改为[1, 2, 3, 4];由于实际上是在更改bc的名称,因此b现在将为b [1, 2, 3, 4].无法将值10更改为其他任何方法. 10永远是10岁,就像吸血鬼克劳迪娅(Claudia)永远是5岁(至少直到她被Kirsten Dunst取代之前).

But in this case, the value is an int, and ints are immutable. In either case, you can rebind a to a different value (e.g., a = "Now I'm a string!"), but the won't affect the original value, which b and c will still be names for. The difference is that with a list, you can change the value [1, 2, 3] into [1, 2, 3, 4] by doing, e.g., a.append(4); since that's actually changing the value that b and c are names for, b will now b [1, 2, 3, 4]. There's no way to change the value 10 into anything else. 10 is 10 forever, just like Claudia the vampire is 5 forever (at least until she's replaced by Kirsten Dunst).

*警告:请勿提供臭名昭著的B.I.G.一个热狗.黑帮说唱僵尸永远不要在午夜之后喂饱.

* Warning: Do not give Notorious B.I.G. a hot dog. Gangsta rap zombies should never be fed after midnight.

这篇关于Python将多个变量分配给相同的值?列出行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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