import copy
lst=[1,2,3,4,[1,2]]

# 复制列表lst,命名lst2
lst2=copy.copy(lst)
print(f'这是lst3:{lst2}')

# 深拷贝
lst3=copy.deepcopy(lst)
print(f'这是lst3:{lst3}')

# 给原来的lst中的最后一个元素(列表)追加元素3
lst[-1].append(3)

print(f'这是增加元素后的原始列表:{lst}')
print(f'这是增加元素后的浅拷贝表:{lst2}')
print(f'这是增加元素后的深拷贝表:{lst3}')

# 通过以上可以看出,列表引用的元素列表改变,浅拷贝是受影响的.

 

相关文章:

  • 2021-05-17
  • 2021-09-22
  • 2022-12-23
  • 2022-02-09
猜你喜欢
  • 2022-12-23
  • 2021-06-10
  • 2021-05-14
  • 2022-12-23
  • 2021-11-23
  • 2021-05-21
相关资源
相似解决方案