gpt4 book ai didi

python - 如何理解这个结果?

转载 作者:太空狗 更新时间:2023-10-30 00:22:25 25 4
gpt4 key购买 nike

我是 Python 新手。这是我对列表的一个问题:据说列表是可变的而元组是不可变的。但是当我写下以下内容时:

L1 = [1, 2, 3]
L2 = (L1, L1)
L1[1] = 5
print L2

结果是

([1, 5, 3], [1, 5, 3])

代替

([1, 2, 3], [1, 2, 3])

但是 L2 是一个元组,而且元组是不可变的。为什么当我改变L1的值时,L2的值也改变了?

最佳答案

来自 Python 文档 (http://docs.python.org/reference/datamodel.html),注意:

The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.

关于python - 如何理解这个结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12231136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com