Python 的list记录
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 names = ["sony","xiaomi","huawei","apple","google"] 5 names2 = ["233","253","163"] 6 7 8 print(names) 9 10 print(names[0]) 11 print(names[-1]) #查询最后1位 12 print(names[-2:]) #查询最后2位 13 14 #追加到最后 15 names.append("TCL") 16 print(names) 17 18 #增加,直接指定需要增加到哪个位置 19 names.insert(1,"alibaba") 20 print(names) 21 22 #修改 23 names[2]="mi" 24 print(names) 25 26 #删除 27 names.remove(names[2]) 28 print(names) 29 names.remove("TCL") 30 print(names) 31 32 #根据名称查询位置 33 print(names.index("apple")) 34 print(names[names.index("apple")]) 35 36 #names和names2合并 37 names.extend(names2) 38 print(names2) 39 print(names) 40 41 #统计次数 42 print(names.count("apple")) 43 44 #list反序 45 print(names) 46 names.reverse() 47 print(names) 48 49 #排序 50 names.sort() 51 print(names) 52 53 #获取1到最后的偶数为内容,0和-1是可以不用写的,2为位数 54 print(names[::2]) #等于 print(names[0:-1:2]) 55 56 #清空列表 57 names.clear() 58 print(names)