Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表操作(是否可变)?_Python_List_Mutable - Fatal编程技术网

Python 列表操作(是否可变)?

Python 列表操作(是否可变)?,python,list,mutable,Python,List,Mutable,我有一段代码: list1 = [1, 2, 3] list2 = list1 list1 = [4, 5, 6] print(list2) print(list1) 这将导致以下输出: [1, 2, 3] [4, 5, 6] 为什么list2还没有指向[4,5,6]?我的印象是,由于列表是可变的,因此更改将同时影响list1和list2,因为在RAM中,两个列表都指向相同的项目序列 如有任何解释,将不胜感激。我已将原因添加为代码中的注释: # list1 variable points

我有一段代码:

list1 = [1, 2, 3]
list2 = list1
list1 = [4, 5, 6]

print(list2)
print(list1)
这将导致以下输出:

[1, 2, 3]
[4, 5, 6]
为什么
list2
还没有指向
[4,5,6]
?我的印象是,由于列表是可变的,因此更改将同时影响
list1
list2
,因为在RAM中,两个列表都指向相同的项目序列


如有任何解释,将不胜感激。

我已将原因添加为代码中的注释:

# list1 variable points to the memory location containing [1, 2, 3]
list1 = [1, 2, 3] 
# list2 variable made to point to the memory location pointed to by list1 
list2 = list1    
# list1 variable made to point to the memory location containing 
# a new list [4, 5, 6], list2 still pointing to [1, 2, 3] 
list1 = [4, 5, 6] 

print(list2) # list2 prints [1, 2, 3]
print(list1) # list1 prints [4, 5, 6]

我已将原因添加为代码中的注释:

# list1 variable points to the memory location containing [1, 2, 3]
list1 = [1, 2, 3] 
# list2 variable made to point to the memory location pointed to by list1 
list2 = list1    
# list1 variable made to point to the memory location containing 
# a new list [4, 5, 6], list2 still pointing to [1, 2, 3] 
list1 = [4, 5, 6] 

print(list2) # list2 prints [1, 2, 3]
print(list1) # list1 prints [4, 5, 6]

我将一行一行地讲:

# Define a list [1, 2, 3] and save it into list1 variable
list1 = [1, 2, 3]

# Define list2 to be equal to list1 (that is, list2 == list1 == [1, 2, 3])
list2 = list1

# Create a new list [4, 5, 6] and save it into list1 variable
# Notice, that this replaces the existing list1!!
list1 = [4, 5, 6]

# Print list2, which still points to the original list [1, 2, 3]
print(list2)

# Print the new list1, [4, 5, 6] that is
print(list1)
然而,这:

list1 = [1, 2, 3]
list2 = list1
list1.append(4)

print(list2)
print(list1)
将输出:

[1, 2, 3, 4]
[1, 2, 3, 4]
由于我们正在编辑
list1
(因此
list2
,它们是可变的),因此不创建新列表并将其保存在变量名
list1


这里的关键字是创建一个新列表,因此您不是在编辑
列表1
在您的示例中,您实际上是在更改名称
列表1
,以指向一个完全不同的列表。

我将逐行介绍:

# Define a list [1, 2, 3] and save it into list1 variable
list1 = [1, 2, 3]

# Define list2 to be equal to list1 (that is, list2 == list1 == [1, 2, 3])
list2 = list1

# Create a new list [4, 5, 6] and save it into list1 variable
# Notice, that this replaces the existing list1!!
list1 = [4, 5, 6]

# Print list2, which still points to the original list [1, 2, 3]
print(list2)

# Print the new list1, [4, 5, 6] that is
print(list1)
然而,这:

list1 = [1, 2, 3]
list2 = list1
list1.append(4)

print(list2)
print(list1)
将输出:

[1, 2, 3, 4]
[1, 2, 3, 4]
由于我们正在编辑
list1
(因此
list2
,它们是可变的),因此不创建新列表并将其保存在变量名
list1


这里的关键字是创建一个新列表,因此您不是在编辑
list1
在您的示例中,您实际上是在更改名称
list1
以指向一个完全不同的列表。

列表是可变的。然而,该行:

list1 = [4, 5, 6]
不会改变以前由
list1
引用的列表对象,它会创建一个全新的列表对象,并将
list1
标识符切换为引用新的列表对象。通过查看对象ID可以看到这一点:

>>> list1 = [1, 2, 3]
>>> list2 = list1
>>> id(list1)
4379272472
>>> id(list2)
4379272472  # both reference same object
>>> list1 = [4, 5, 6]
>>> id(list1)
4379279016  # list1 now references new object
>>> id(list2)
4379272472  # list2 still references previous object

列表是可变的。然而,该行:

list1 = [4, 5, 6]
不会改变以前由
list1
引用的列表对象,它会创建一个全新的列表对象,并将
list1
标识符切换为引用新的列表对象。通过查看对象ID可以看到这一点:

>>> list1 = [1, 2, 3]
>>> list2 = list1
>>> id(list1)
4379272472
>>> id(list2)
4379272472  # both reference same object
>>> list1 = [4, 5, 6]
>>> id(list1)
4379279016  # list1 now references new object
>>> id(list2)
4379272472  # list2 still references previous object

相关:事实上,如果将“传递给函数”替换为“分配给变量”,它基本上回答了这个问题。相关:事实上,如果将“传递给函数”替换为“分配给变量”,它基本上回答了这个问题。也许,我不应该使用内联注释,它们很难阅读@帕德雷坎宁厄姆非常感谢帕迪,你的编辑拯救了马天!6分钟内投3票(35分钟内没有)似乎不可能?不再是。。。。再次感谢!!!不用担心,发票在邮寄中;)也许,我不应该使用内联评论,它们很难阅读@帕德雷坎宁厄姆非常感谢帕迪,你的编辑拯救了马天!6分钟内投3票(35分钟内没有)似乎不可能?不再是。。。。再次感谢!!!不用担心,发票在邮寄中;)