标签:常用 等于 代码 out 没有 内存 style [1] import
In [1]: a = 1
In [2]: b = a
In [3]: a
Out[3]: 1
In [4]: b
Out[4]: 1
In [5]: id(a)
Out[5]: 4452948992
In [6]: id(b)
Out[6]: 4452948992
In [7]: id(1)
Out[7]: 4452948992
In [8]: a = 4
In [9]: b
Out[9]: 1
In [10]: id(a)
Out[10]: 4452949088
In [11]: id(b)
Out[11]: 4452948992
In [12]: id(1)
Out[12]: 4452948992
In [1]: li = [1,2,3]
In [2]: n = li
In [3]: li
Out[3]: [1, 2, 3]
In [4]: n
Out[4]: [1, 2, 3]
In [5]: li[0] = ‘a‘
In [6]: li
Out[6]: [‘a‘, 2, 3]
In [7]: n
Out[7]: [‘a‘, 2, 3]
In [8]: id(li)
Out[8]: 4342626376
In [9]: id(n)
Out[9]: 4342626376
In [10]: id([1,2,3])
Out[10]: 4440762824
可以看出,在列表中,n是指向了li的内存地址, 不是列表[1,2,3]了,所以li的值变化的话,n也跟着变化。可以想象成li和n都指向了一个水杯,水杯里有三块白糖,把其中一块换成了黑糖,
那不管是用li还是n,水杯里现在都是有两块白糖和一块黑糖。
2.浅copy
In [1]: li2 = [1,2,3,[‘alex‘,27]]
In [2]: n2 = li2.copy()
In [3]: li2
Out[3]: [1, 2, 3, [‘alex‘, 27]]
In [4]: n2
Out[4]: [1, 2, 3, [‘alex‘, 27]]
In [5]: id(li2),id(n2)
Out[6]: (4536317192, 4536271560)
In [6]: li2[0] = ‘a‘
In [7]: li2
Out[7]: [‘a‘, 2, 3, [‘alex‘, 27]]
In [8]: n2
Out[8]: [1, 2, 3, [‘alex‘, 27]]
In [9]: id(li2[0]),id(n2[0])
Out[9]: (4509371784, 4506651648)
In [10]: li2[3][0] = ‘帅哥‘
In [11]: li2
Out[11]: [‘a‘, 2, 3, [‘帅哥‘, 27]]
In [12]: n2
Out[12]: [1, 2, 3, [‘帅哥‘, 27]]
In [13]: id(li2[3]),id(n2[3])
Out[13]: (4536316872, 4536316872)
In [15]: id(li2[3][0])
Out[15]: 4536362680 # 和li2[3]的内存地址不一样
In [1]: import copy
In [2]: li3 = [1,2,3,[‘alex‘,27]]
In [3]: n3 = copy.deepcopy(li3)
In [4]: li3
Out[4]: [1, 2, 3, [‘alex‘, 27]]
In [5]: n3
Out[5]: [1, 2, 3, [‘alex‘, 27]]
In [6]: li3[3][0] = ‘帅哥‘
In [7]: li3
Out[7]: [1, 2, 3, [‘帅哥‘, 27]]
In [8]: n3
Out[8]: [1, 2, 3, [‘alex‘, 27]]
标签:常用 等于 代码 out 没有 内存 style [1] import
原文地址:https://www.cnblogs.com/lshedward/p/9912081.html