列表的操作:增删改查以及列表的三种拷贝方式
1 names = ["tianjie","yecai","haitao","xinlin"] 2 names.append("tianjie") #列表在尾部新增 3 print(names) 4 print(names[0],names[2]) 5 print(names[1:3]) 6 # 查 7 print(names[-1]) 8 print(names[-2:]) 9 print(names[(names.index("xinlin"))]) #list.index("x"),求x的索引 10 print(names.count("tianjie")) #计算列表中“tianjie”的数量 11 names.clear()#清空 12 names.reverse()#反转 13 names.sort()#排序 14 name2 = [1, 2, 3, 4] 15 names.extend(name2) #在尾部一次性增加另一个列表的所有元素 16 print(names) 17 del name2 #删除整个列表 18 print(name2) 19 names2 = names.copy() #浅拷贝 20 print(names2) 21 print(names) 22 # 增 23 names.insert(1,"lubei") 24 print(names) 25 names.insert(3, "xin") 26 print(names) 27 # 改 28 names[2] = "xiedi" 29 print(names) 30 # 删 31 names.remove("lubei") 32 print(names) 33 del names[1] 34 print(names) 35 names.pop(1) 36 print(names) 37 38 # 列表的拷贝 39 import copy 40 41 person=['name',['saving',100]] 42 p1=copy.copy(person) 43 print(p1) 44 p2=person[:] 45 print(p2) 46 p3=list(person) 47 print(p3)