首页 > 代码库 > 列表链接是赋值,如果要不影响原来的可以用切片赋值

列表链接是赋值,如果要不影响原来的可以用切片赋值

例子1:

In [17]: nums = range(10)

In [18]: nums_temp = nums

In [20]: nums_temp[0] = "comk"

In [21]: nums

Out[21]: [‘comk‘, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [22]: nums_temp

Out[22]: [‘comk‘, 1, 2, 3, 4, 5, 6, 7, 8, 9]


例子2:

In [23]: nums = range(10)

In [25]: nums_temp = nums[:]

In [26]: nums_temp[0] = "comk"

In [27]: nums

Out[27]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [28]: nums_temp

Out[28]: [‘comk‘, 1, 2, 3, 4, 5, 6, 7, 8, 9]


本文出自 “净空蓝星” 博客,请务必保留此出处http://jingkonglanxing.blog.51cto.com/1152128/1910315

列表链接是赋值,如果要不影响原来的可以用切片赋值