1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| 1、用途:按位置存放多个值
2、定义方式:在[]内用逗号分割开多个任意类型的元素 l = [11,3.3,"xx",[1111,2222]] print(type(l))
list数据类型转换: 所有可以被for循环遍历的类型都可以传给list然后转换成列表 print(list("abc")) print(list({"k1":111,"k2":222}))
res=list(range(5)) print(res)
3、常用操作+内置的方法 优先掌握的操作: 1、按索引存取值(正向存取+反向存取):即可存也可以取 list1 = [111,222,333,444,555] print(list1[0]) print(list1[-1])
print(id(list1)) list1[0] = 666 print(id(list1))
list1[5] = 777
2、切片(顾头不顾尾,步长) list1 = [111,222,333,444,555] print(list1[0:3]) print(list1)
l=list1[:] print(l)
print(list1[::-1])
3、长度 list1 = [111,222,333,444,555] print(len(list1))
4、成员运算in和not in list1 = [111,222,333,444,555] print(222 in list1)
5、追加 list1 = [111,222,333,444,555] list1.append(666) x = list1.append(777) print(list1) print(x)
list1.insert(2,666) print(list1)
6、删除 list1 = [111,222,333,444,555] (1) 单纯的删除-》没有返回值 del list1[0] print(list1)
res=list1.remove(333) print(list1) print(res)
(2)取走-》有返回值 res=list1.pop(1) print(list1)
print(res) 结果: 222
7、循环 list1 = [111,222,333,444,555] for x in list1: print(x)
========================需要掌握的操作 list1 = [111,222,333,333,333,444,555] print(list1.count(333))
list1= [99,33,22,11,-3,87] list1.reverse() print(list1)
list1 = [111,222,333,333,333,444,555] list1.clear() list1.append([666,777,888])
for x in [666,777,888]: list1.append(x) list1.extend([666,777,888]) print(list1)
浅copy list1.copy() list1[:]
list1 = [111,222,777,888,999,333,333,333,444,555] res=list1.index(333,0,3) print(res)
list1 = [111,222,777,888,999,333,333,333,444,555] list1.sort(reverse=True) print(list1)
queue = []
queue.append('one') queue.append('two') queue.append('three') print(queue)
print(queue.pop(0)) print(queue.pop(0)) print(queue.pop(0))
|