python - list1 在我的代码中的任何地方都不会被分配等于 list2,但是 list1 的值仍然被复制到 list2
问题描述
我尝试使用一些打印语句来调试代码。所以,我在这里展示整个代码。
l = ['one', 'two', 'three', 'four', 'five']
l1 = list()
count = 0
l2 = list()
for r in range(len(l)):
print(r, 'this is r')
print()
if count < 2:
l1.append(l[r])
print(l1)
print(l2) #when the loop runs for the third time l2 is automatically updating to l1, which
#is now ['one','two,'three'].Why?
count += 1
if count == 2:
l2.append(l1)
print(l2, 'this is l2')
print()
count = 0
print(l2)
我认为 l2 的输出应该是: [['one', 'two'], ['one', 'two', 'three', 'four']]
但我得到的输出是: [['one', 'two', 'three', 'four', 'five'], ['one', 'two', 'three', 'four', 'five ']]
解决方案
推荐阅读
- java - 为什么 JsonCreator 不适用于 JsonFormat 形状对象
- r - 数字与表格的乘法
- python - 将 Python 字典列表映射到 Matlab 结构
- java - 从 URL 读取文件并将其写入磁盘时提高性能
- c# - 来自 Vue 应用程序的发布请求被 CORS 策略阻止
- powershell - Azure 自动化 | 运行手册 | 外壳 | 获取 AzRoleAssignment | Microsoft.Rest.Azure.CloudException
- android - 语言名称和代码中的 Hashmap 键值对
- c++ - 当无法知道长度或将其作为参数传递时,有没有办法在模板类中创建指针样式数组?
- google-bigquery - BigQuery 文字列名称
- powershell - 如何为我在 powershell 中创建的类定义默认排序顺序?