python中a=x和a=x[:]有什么区别 [英] what is the difference between a = x and a=x[:] in python
本文介绍了python中a=x和a=x[:]有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试学习 Python.有人可以帮助我理解以下两者之间的区别:a = x vs a=x[:]
I am trying to learn Python. Can someone please help me understand the difference between following two: a = x vs a=x[:]
推荐答案
a = x
创建引用:
a = [2]
x = a
print id(a)
print id(x)
产生:
39727240
39727240
所以如果你改变 a
那么 x
也会改变,因为它们是相同的对象
So if you alter a
then x
would change too because they are the same objects
鉴于
a = x[:]
创建一个新对象
a = [2]
x = a[:]
print id(a)
print id(x)
产生:
41331528
39722056
但是在这里改变 a
不会改变 x
因为它们是不同的对象
But over here changing a
doesn't alter x
because they are different objects
这篇关于python中a=x和a=x[:]有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文