网站搜索优化技巧,珠海网站制作,做购物网站的素材,芜湖企业100强一、列表 列表是一个可以包含所以数据类型的对象的位置有序集合#xff0c;它是可以改变的。 1、列表的序列操作#xff08;Python3#xff09; 123456789101112131415161718192021222324 one_list [1,2,3,4] two_list [jonny,… 一、列表 列表是一个可以包含所以数据类型的对象的位置有序集合它是可以改变的。 1、列表的序列操作Python3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 one_list [1,2,3,4] two_list [jonny,jery,david] #统计元素数量 len(one_list) 4 #索引根据偏移量左起从0开始右起从-1开始 one_list[0] 1 one_list[-1] 4 #切片 one_list[0:2] [1, 2] one_list[:-1] [1, 2, 3] one_list[1:] [2, 3, 4] #步进默认为1 one_list[::2] [1, 3] #扩展进来新的列表 new_list one_list two_list print(new_list) [1, 2, 3, 4, jonny, jery, david] 2、列表的方法 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 #在列表中加入元素 one_list.append(join) print(one_list) [1, 2, 3, 4, join] #查看元素在列表中的偏移量 one_list.index(join) 4 #统计元素在列表中出现的次数 one_list [1,2,3,4,join,jonny,3,4,2,45,32,gg,45] one_list.count(3) 2 #在列表中指定位置插入元素 one_list.insert(1,insert_ele) print(one_list) [1, insert_ele, 2, 3, 4, join, jonny, 3, 4, 2, 45, 32, gg, 45] #移除指定元素 one_list.remove(insert_ele) print(one_list) [1, 2, 3, 4, join, jonny, 3, 4, 2, 45, 32, gg, 45] #通过附加来自可迭代的元素扩展列表字符串列表元组等 one_list.extend(extend) print(one_list) [1, 2, 3, 4, join, jonny, 3, 4, 2, 45, 32, gg, 45, e, x, t, e, n, d] #移除指定偏移量的元素不指定则为随机移除 one_list [1,2,3,4] one_list.pop() 4 one_list.pop(1) 2 print(one_list) [1, 3] #根据ASCII码排序python2.X系列可对所有元素排序3.X系列只能对相同类型元素排序 Python3.6 one_list [3,6,2,8] one_list.sort() print(one_list) [2, 3, 6, 8] Python2.7 two_list [3,6,4,7] two_list.extend(djttdkx01) print two_list [3, 6, 4, 7, d, j, t, t, d, k, x, 0, 1] two_list.sort() print two_list [3, 4, 6, 7, 0, 1, d, d, j, k, t, t, x] #反转列表 two_list.reverse() print two_list [x, t, t, k, j, d, d, 1, 0, 7, 6, 4, 3] #列表的复制方法一复制第一级对于嵌套的列表只是复制其引用位置 one_list [1,2,3,4,[5,6,7,8]] two_list one_list[:] print(two_list) [1, 2, 3, 4, [5, 6, 7, 8]] id(one_list) 5697352 id(two_list) 50197576 #列表复制方法二复制第一级对于嵌套的列表只是复制其引用位置 three_list one_list.copy() print(three_list) [1, 2, 3, 4, [5, 6, 7, 8]] id(three_list) 49960008 #列表复制方法三copy模块的深度复制 import copy four_list copy.deepcopy(one_list) print(four_list) [1, 2, 3, 4, [5, 6, 7, 8]] one_list[4][0] 55 print(two_list) [1, 2, 3, 4, [55, 6, 7, 8]] print(three_list) [1, 2, 3, 4, [55, 6, 7, 8]] print(four_list) [1, 2, 3, 4, [5, 6, 7, 8]] 3、列表的嵌套 1 2 3 4 5 one_list [2,3,1,7,[2,gg,david],87,98] one_list[4][1][1] g one_list[4][2] david 4、列表解析 1 2 3 4 5 6 7 8 one_list [[1,2,3],[4,5,6],[7,8,9]] new_list [row[0] for row in one_list] print(new_list) [1, 4, 7] two_list [row[1] % 2 for row in one_list] print(two_list) [0, 1, 0] 5、练习 1 2 3 4 5 6 7 8 9 10 11 12 13 练习找列表中的9替换成9999 同时找出所有的34全删掉 one_list [jym,alex,9,jonny,sun,3,6,7,8,2,3,1,9,34,543,43,32,34,gg,jids] print(one_list) for i in range(one_list.count(9)): one_list [one_list.index(9)] 9999 for i in range(one_list.count(34)): del one_list[one_list.index(34)] print(one_list) 二、元组 元组是不可改变的列表编写在圆括号中支持任意类型任意嵌套等常见操作 1、元组的序列操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 one_tuple (1,2,3,4) #统计元素个数 len(one_tuple) 4 #元组附加 two_tuple one_tuple (5,6) print(two_tuple) (1, 2, 3, 4, 5, 6) #索引 one_tuple[0] 1 one_tuple[-2] 3 2、元组的方法 1 2 3 4 5 6 7 #查看元素在元组中的偏移量 one_tuple.index(2) 1 #统计元素在元组中出现的次数 one_tuple.count(2) 1 3、元组的嵌套 元组本身的元素是不可被修改的但元组中嵌套的字典或列表的元素是可变的。 1 2 3 4 5 6 7 8 9 t1 (1,2,{k1:v1}) t1[2][k1] v2 print(t1) (1, 2, {k1: v2}) t2 (1,2,[1,2]) t2[2][0] new1 print(t2) (1, 2, [new1, 2]) 本文转自 元婴期 51CTO博客原文链接:http://blog.51cto.com/jiayimeng/1897851