上节简单地介绍了一下python列表的定义以及增删改查操作,这节我们继续学习列表的其他操作,希望有不正确的地方可以得到大家的补充,指正。
列表的拷贝
依旧按照上次的例子,定义一个列表
namelist =["studentA", "studentB", "studentC", "studentD", "studentE"]
那么下面我们拷贝这个列表,定义为namelist2
namelist =["studentA", "studentB", "studentC", "studentD", "studentE"]
namelist2 = namelist.copy()
print(namelist2)
运行结果:
['studentA', 'studentB', 'studentC', 'studentD', 'studentE']
以上就是列表copy方法的基本使用,其实copy还分为深copy和浅copy
我现在定义一个列表,其中的元素也包含一个列表
namelist =["studentA", "studentB", "studentC",["Q", "S"], "studentD", "studentE"]
下面我们演示一下浅copy
namelist2 = namelist.copy() # 只是浅copy,只