首页 > 解决方案 > python中一个变量的内容何时与另一个变量的内容保持不变

问题描述

#create list
listA = [1,2]
#'duplicate' list
listB = listA
#remove item in 'duplicate'
listB.remove(1)
#item has been removed from listA even though it hasn't been edited
print(listA)
# Result: [2]

从逻辑上讲,我会假设 listA 不会被编辑。因为我没有直接编辑它。因此,如果 python 指向变量“内容”的位置,那么我可以理解它。因为 listB 不是真正的“重复”,而是另一个指向同一组信息的“路标”。但如果那是真的,那为什么:

#

create variable
intA = 5
#'duplicate' variable
intB = intA
#chane item in 'duplicate'
intB = 0
#item has NOT been removed from listA because it hasn't been edited
print( 

整数)

标签: pythonlistvariablesmutable

解决方案


推荐阅读