#传参列表副本(不改变列表本身)
lst = [1, 5, 33, 58]

def func(a):
    a[0] = 99
    print(a)

print(lst)              #[1, 5, 33, 58]
func(lst[:])            #不改变lst, [99, 5, 33, 58]
func(lst.copy())     #不改变lst, [99, 5, 33, 58]
print(lst)               #[1, 5, 33, 58]

func(lst)   #改变lst, [99, 5, 33, 58]
print(lst)  #[99, 5, 33, 58]

打印结果:
[1, 5, 33, 58]
[99, 5, 33, 58]
[99, 5, 33, 58]
[1, 5, 33, 58]
[99, 5, 33, 58]
[99, 5, 33, 58]

 

相关文章:

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