1、浅COPY,如果源变化,p1[1][1]、p2[1][1]、person[1][1]会一起变化
import copyperson = [‘name‘,[‘saving‘,100]]‘‘‘p1 = copy.copy(person)p2 = person[:]p3 = list(person)‘‘‘p1 = person[:]p2 = person[:]print(p1)print(p2) p1[0] = ‘alex‘p2[0] = ‘fengjie‘print(p1)print(p2) p1[1][1] = ‘50‘print(p1)print(p2) 1、深copy:源变化,不影响
import copynames2 = copy.deepcopy(names)print(names)print(names2)names[2] = "湘碰"names[3][0] = "ALEC"print(names)print(names2) 3、元组:不可以编辑的列表,其他操作同列表
names = (‘alex‘, ‘jack‘) 4、列表操作
#列表类型names = ["Zhangyang","Guyun","Xiangpeng",["alex","jack"],"xulianghen"]names2 = ["1","2","3","4"]print(names)#print(names[0]) #第一个位置#print(names[0],names[2])#print(names[1:3]) #切片,顾头不顾尾#print(names[1:]) #取从1到结束位置#print(names[-1]) #取最后一个#print(names[-2:-1]) #不包含最后一个值,顾头不顾尾#print(names[:3]) #0可以忽略 #names.append("Leihaidong") #插入到最后面#names.insert(1,"Chenronghua") #插入到固定位置 ,一次只能插入一个 #names[2] = "Xiedi" #修改 #删除#names.remove("Zhangyang")#del names[1]#names.pop(1) #不输入默认删除最后一个 #print(names.index("Guyun")) #找Guyun的下标#print( names[names.index("Guyun")])#统计Guyun个数#print(names.count("Guyun"))#names.clear() #清空#names.reverse() #反转#names.sort() #排序#names.extend(names2) #扩展#del names2 #删除变量
原文地址:https://www.cnblogs.com/wzsx/p/8949631.html
时间: 2024-09-29 04:11:21