python - 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(
整数)
解决方案
推荐阅读
- c++ - 如何在最后一个子 fork() 下打印带有名称和 pid 的子进程
- clips - 如何使用 CLIPS 读取/使用用户输入
- amazon-quicksight - 是否可以在 Amazon Quicksight 中复制视觉对象?
- html - Powershell ConvertTo-HTML 缩小表格单元格中的图像
- c# - 用于将表与视图模型连接起来的 EF6 Linq 查询
- python - 小部件未显示在滚动区域中
- google-apps-script - 勾选复选框触发添加时间戳的功能
- android - 以 Android Q 为目标时构建失败,但在 API 28 上工作时出现“无法将文件 'android.jar' 转换为匹配属性”
- javascript - 使用 React 路由器更轻松地通过侧边栏导航动态呈现组件
- elasticsearch - 如何在 Windows Server 中使用 ElasticSearch 配置 Jaeger 收集器