问题:Python将多个变量分配给相同的值?列出行为
我试图使用如下所示的多重赋值来初始化变量,但是我对此行为感到困惑,我希望分别重新赋值列表,我的意思是b [0]和c [0]等于0。
a=b=c=[0,3,5]
a[0]=1
print(a)
print(b)
print(c)
结果是:[1、3、5] [1、3、5] [1、3、5]
那是对的吗?多重分配应该使用什么?有什么不同呢?
d=e=f=3
e=4
print('f:',f)
print('e:',e)
结果:(’f:’,3)(’e:’,4)
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)
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)
result:
(‘f:’, 3)
(‘e:’, 4)
回答 0
如果您要使用C / Java / etc等语言的Python。家庭,这可能会帮助您停止将其a
视为“变量”,而开始将其视为“名称”。
a
,,b
和c
不是具有相等值的不同变量;它们是相同名称的不同名称。变量具有类型,身份,地址和类似的东西。
名称没有任何名称。值当然可以,并且对于相同的值,您可以有很多名称。
如果给Notorious B.I.G.
热狗* Biggie Smalls
,请Chris Wallace
带一个热狗。如果将的第一个元素更改a
为1,则b
和的第一个元素为c
1。
如果您想知道两个名称是否在命名同一对象,请使用is
运算符:
>>> a=b=c=[0,3,5]
>>> a is b
True
然后您问:
有什么不同呢?
d=e=f=3
e=4
print('f:',f)
print('e:',e)
在这里,您将名称重新绑定e
到value 4
。这并不影响名称d
和f
以任何方式。
在先前的版本中,您分配给a[0]
,而不是a
。因此,从的角度来看a[0]
,您正在重新绑定a[0]
,但是从的角度来看a
,您正在就位更改它。
您可以使用该id
函数,该函数为您提供代表对象身份的唯一编号,以准确查看哪个对象是哪个对象,即使在is
无济于事的情况下:
>>> 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]
现在也是该新值的名称。这是因为a
和b
仍在命名同一对象。
在幕后,a[0]=1
实际上是在列表对象上调用方法。(等效于a.__setitem__(0, 1)
。)因此,它实际上根本没有重新绑定任何内容。这就像打电话my_object.set_something(1)
。当然,该对象可能是重新绑定实例属性以实现此方法,但这并不重要。重要的是您没有分配任何东西,只是在改变对象。与相同a[0]=1
。
user570826询问:
如果有的话 a = b = c = 10
这与以下情况完全相同a = b = c = [1, 2, 3]
:您有三个名称相同的值。
但是在这种情况下,值是an int
,而int
s是不可变的。在这两种情况下,你可以重新绑定a
到一个不同的值(例如,a = "Now I'm a string!"
),但不会影响原来的值,这b
与c
将仍然是名称。不同的是,有一个列表,你可以更改值[1, 2, 3]
到[1, 2, 3, 4]
这样做,例如a.append(4)
; 由于那实际上是在更改值b
和c
的名称,b
因此现在b [1, 2, 3, 4]
。无法将值更改10
为其他任何值。10
永远是10,就像克劳迪娅(Claudia)一样,吸血鬼也永远是5(至少直到她被Kirsten Dunst取代)。
*警告:请勿给臭名昭著的BIG热狗。黑帮说唱僵尸永远不要在午夜之后喂饱。
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”.
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.
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.
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)
Here, you’re rebinding the name e
to the value 4
. That doesn’t affect the names d
and f
in any way.
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.
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
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.
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 asked:
What if we have, a = b = c = 10
That’s exactly the same situation as a = b = c = [1, 2, 3]
: you have three names for the same value.
But in this case, the value is an int
, and int
s 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).
* Warning: Do not give Notorious B.I.G. a hot dog. Gangsta rap zombies should never be fed after midnight.
回答 1
咳嗽
>>> a,b,c = (1,2,3)
>>> a
1
>>> b
2
>>> c
3
>>> a,b,c = ({'test':'a'},{'test':'b'},{'test':'c'})
>>> a
{'test': 'a'}
>>> b
{'test': 'b'}
>>> c
{'test': 'c'}
>>>
Cough cough
>>> a,b,c = (1,2,3)
>>> a
1
>>> b
2
>>> c
3
>>> a,b,c = ({'test':'a'},{'test':'b'},{'test':'c'})
>>> a
{'test': 'a'}
>>> b
{'test': 'b'}
>>> c
{'test': 'c'}
>>>
回答 2
是的,这是预期的行为。a,b和c都设置为同一列表的标签。如果需要三个不同的列表,则需要分别分配它们。您可以重复显示列表,也可以使用多种方式之一复制列表:
b = a[:] # this does a shallow copy, which is good enough for this case
import copy
c = copy.deepcopy(a) # this does a deep copy, which matters if the list contains mutable objects
Python中的赋值语句不复制对象-它们将名称绑定到对象,并且对象可以具有与您设置的一样多的标签。在第一次编辑中,更改a [0],您将更新a,b和c均引用的单个列表中的一个元素。在第二次更改e中,您将e切换为另一个对象的标签(4而不是3)。
Yes, that’s the expected behavior. a, b and c are all set as labels for the same list. If you want three different lists, you need to assign them individually. You can either repeat the explicit list, or use one of the numerous ways to copy a list:
b = a[:] # this does a shallow copy, which is good enough for this case
import copy
c = copy.deepcopy(a) # this does a deep copy, which matters if the list contains mutable objects
Assignment statements in Python do not copy objects – they bind the name to an object, and an object can have as many labels as you set. In your first edit, changing a[0], you’re updating one element of the single list that a, b, and c all refer to. In your second, changing e, you’re switching e to be a label for a different object (4 instead of 3).
回答 3
在python中,一切都是对象,也是“简单”的变量类型(int,float等)。
当您更改变量值时,实际上是在更改其指针,如果在两个变量之间进行比较,则会对其指针进行比较。(要清楚,指针是物理计算机内存中存储变量的地址)。
结果,当您更改内部变量值时,会更改其在内存中的值,并且会影响指向该地址的所有变量。
例如,当您这样做时:
a = b = 5
这意味着a和b指向内存中包含值5的相同地址,但是当您这样做时:
a = 6
它不会影响b,因为a现在指向另一个包含6的存储位置,而b仍然指向包含5的存储地址。
但是,当您这样做时:
a = b = [1,2,3]
a和b再次指向相同的位置,但是不同之处在于,如果更改列表值之一:
a[0] = 2
它会更改a所指向的内存的值,但是a仍然指向与b相同的地址,结果b也将更改。
In python, everything is an object, also “simple” variables types (int, float, etc..).
When you changes a variable value, you actually changes it’s pointer, and if you compares between two variables it’s compares their pointers.
(To be clear, pointer is the address in physical computer memory where a variable is stored).
As a result, when you changes an inner variable value, you changes it’s value in the memory and it’s affects all the variables that point to this address.
For your example, when you do:
a = b = 5
This means that a and b points to the same address in memory that contains the value 5, but when you do:
a = 6
It’s not affect b because a is now points to another memory location that contains 6 and b still points to the memory address that contains 5.
But, when you do:
a = b = [1,2,3]
a and b, again, points to the same location but the difference is that if you change the one of the list values:
a[0] = 2
It’s changes the value of the memory that a is points on, but a is still points to the same address as b, and as a result, b changes as well.
回答 4
您可以id(name)
用来检查两个名称是否代表相同的对象:
>>> a = b = c = [0, 3, 5]
>>> print(id(a), id(b), id(c))
46268488 46268488 46268488
列表是可变的;这意味着您可以在不创建新对象的情况下就地更改值。但是,这取决于您如何更改值:
>>> a[0] = 1
>>> print(id(a), id(b), id(c))
46268488 46268488 46268488
>>> print(a, b, c)
[1, 3, 5] [1, 3, 5] [1, 3, 5]
如果您将新列表分配给a
,则其ID将更改,因此不会影响b
和c
的值:
>>> a = [1, 8, 5]
>>> print(id(a), id(b), id(c))
139423880 46268488 46268488
>>> print(a, b, c)
[1, 8, 5] [1, 3, 5] [1, 3, 5]
整数是不可变的,因此您不能在不创建新对象的情况下更改值:
>>> x = y = z = 1
>>> print(id(x), id(y), id(z))
507081216 507081216 507081216
>>> x = 2
>>> print(id(x), id(y), id(z))
507081248 507081216 507081216
>>> print(x, y, z)
2 1 1
You can use id(name)
to check if two names represent the same object:
>>> a = b = c = [0, 3, 5]
>>> print(id(a), id(b), id(c))
46268488 46268488 46268488
Lists are mutable; it means you can change the value in place without creating a new object. However, it depends on how you change the value:
>>> a[0] = 1
>>> print(id(a), id(b), id(c))
46268488 46268488 46268488
>>> print(a, b, c)
[1, 3, 5] [1, 3, 5] [1, 3, 5]
If you assign a new list to a
, then its id will change, so it won’t affect b
and c
‘s values:
>>> a = [1, 8, 5]
>>> print(id(a), id(b), id(c))
139423880 46268488 46268488
>>> print(a, b, c)
[1, 8, 5] [1, 3, 5] [1, 3, 5]
Integers are immutable, so you cannot change the value without creating a new object:
>>> x = y = z = 1
>>> print(id(x), id(y), id(z))
507081216 507081216 507081216
>>> x = 2
>>> print(id(x), id(y), id(z))
507081248 507081216 507081216
>>> print(x, y, z)
2 1 1
回答 5
在第一个示例中,a = b = c = [1, 2, 3]
您实际上是在说:
'a' is the same as 'b', is the same as 'c' and they are all [1, 2, 3]
如果要将“ a”设置为1,将“ b”设置为“ 2”,将“ c”设置为3,请尝试以下操作:
a, b, c = [1, 2, 3]
print(a)
--> 1
print(b)
--> 2
print(c)
--> 3
希望这可以帮助!
in your first example a = b = c = [1, 2, 3]
you are really saying:
'a' is the same as 'b', is the same as 'c' and they are all [1, 2, 3]
If you want to set ‘a’ equal to 1, ‘b’ equal to ‘2’ and ‘c’ equal to 3, try this:
a, b, c = [1, 2, 3]
print(a)
--> 1
print(b)
--> 2
print(c)
--> 3
Hope this helps!
回答 6
简而言之,在第一种情况下,您要为分配多个名称list
。在内存中仅创建一个列表副本,并且所有名称均指向该位置。因此,使用任何名称更改列表实际上都会修改内存中的列表。
在第二种情况下,将在内存中创建相同值的多个副本。因此,每个副本彼此独立。
Simply put, in the first case, you are assigning multiple names to a list
. Only one copy of list is created in memory and all names refer to that location. So changing the list using any of the names will actually modify the list in memory.
In the second case, multiple copies of same value are created in memory. So each copy is independent of one another.
回答 7
您需要的是:
a, b, c = [0,3,5] # Unpack the list, now a, b, and c are ints
a = 1 # `a` did equal 0, not [0,3,5]
print(a)
print(b)
print(c)
What you need is this:
a, b, c = [0,3,5] # Unpack the list, now a, b, and c are ints
a = 1 # `a` did equal 0, not [0,3,5]
print(a)
print(b)
print(c)
回答 8
执行我需要的代码可能是这样的:
# test
aux=[[0 for n in range(3)] for i in range(4)]
print('aux:',aux)
# initialization
a,b,c,d=[[0 for n in range(3)] for i in range(4)]
# changing values
a[0]=1
d[2]=5
print('a:',a)
print('b:',b)
print('c:',c)
print('d:',d)
结果:
('aux:', [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])
('a:', [1, 0, 0])
('b:', [0, 0, 0])
('c:', [0, 0, 0])
('d:', [0, 0, 5])
The code that does what I need could be this:
# test
aux=[[0 for n in range(3)] for i in range(4)]
print('aux:',aux)
# initialization
a,b,c,d=[[0 for n in range(3)] for i in range(4)]
# changing values
a[0]=1
d[2]=5
print('a:',a)
print('b:',b)
print('c:',c)
print('d:',d)
Result:
('aux:', [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])
('a:', [1, 0, 0])
('b:', [0, 0, 0])
('c:', [0, 0, 0])
('d:', [0, 0, 5])