2012-02-23 59 views
6

我找不到什么在这个非常简单的程序正在进行简洁的信息:Python变量处理,我不明白它

print 'case 1' 
# a and b stay different 
a = [1,2,3] 
b = a 
b = [4,5,6] 

print 'a =',a 
print 'b =',b 

print 
print 'case 2' 
# a and b becomes equal 
a = [1,2,3] 
b = a 
b[0] = 4 
b[1] = 5 
b[2] = 6 

print 'a =',a 
print 'b =',b 
print 

print 'case 3' 
# a and b stay different now 
a = [1,2,3] 
b = a[:] 
b[0] = 4 
b[1] = 5 
b[2] = 6 
print 'a =',a 
print 'b =',b 
print 

print 'case 4' 
# now the funny thing 
a=[1,2,[3]] 
b=a[:] 
b[0] = 4 
b[1] = 5 
b[2][0] = 6 # this modifies b and a!!! 

这个简单的测试的输出是:

case 1 
a = [1, 2, 3] 
b = [4, 5, 6] 

case 2 
a = [4, 5, 6] 
b = [4, 5, 6] 

case 3 
a = [1, 2, 3] 
b = [4, 5, 6] 

case 4 
a = [1, 2, [6]] 
b = [4, 5, [6]] 

我显然不明白python如何处理每种情况。任何人都可以提供一个链接,以便我可以阅读它,或者对发生的事情做一个简短的解释?

非常感谢。

回答

6

这里有两个重要的事情:

  • 变量只是标签指向对象
  • 名单在Python可变和整数都没有。

当您发现ab都被修改时,那是因为它们都指向同一个对象。您可以使用id(a)id(b)来确认。

关于这些示例,请注意a[:]将创建一个新对象,它是a的副本。但是,这将是一个浅层副本,而不是深层副本。这解释了为什么在示例4中,您仍然可以修改ab。它们指向不同的列表对象,但其中一个元素是另一个由它们共享的列表。

4

案例1:名称b被反弹。

案例2:ab被绑定到相同的对象。

案例3:a的浅拷贝绑定到b。列表不同,但列表中的对象是相同的。

案例4:a的浅拷贝绑定到b,然后其中一个对象发生了变异。

重新结合不会突变,并且突变不会重新结合。

3
print 'case 1' 
# a and b stay different 
a = [1,2,3] 
b = a    #At this point 'b' and 'a' are the same, 
        #just names given to the list 
b = [4,5,6]  #At this point you assign the name 'b' to a different list 

print 'a =',a 
print 'b =',b 

print 
print 'case 2' 
# a and b becomes equal 
a = [1,2,3]  #At this point 'b' and 'a' are the same, 
        #just names given to the list 
b = a 
b[0] = 4   #From here you modify the list, since both 'a' and 'b' 
        #reference the same list, you will see the change in 'a' 
b[1] = 5 
b[2] = 6 


print 'case 3' 
# a and b stay different now 
a = [1,2,3] 
b = a[:]    #At this point you COPY the elements from 'a' into a new 
         #list that is referenced by 'b' 
b[0] = 4    #From here you modify 'b' but this has no connection to 'a' 
b[1] = 5 
b[2] = 6 
print 'a =',a 
print 'b =',b 
print 

print 'case 4' 
# now the funny thing 
a=[1,2,[3]] 
b=a[:]   #At this point you COPY the elements from 'a' into a new 
       #list that is referenced by 'b' 
b[0] = 4  #Same as before 
b[1] = 5 
b[2][0] = 6 # this modifies b and a!!! #Here what happens is that 'b[2]' holds 
       #the same list as 'a[2]'. Since you only modify the element of that 
       #list that will be visible in 'a', try to see it as cases 1/2 just 
       #'recursively'. If you do b[2] = 0, that won't change 'a' 
+0

杰出的解释。这对我非常有帮助,尤其是列表[:]声明。谢谢。 – trinkner 2017-12-03 21:27:32