一、列表
????列表是一個可以包含所以數據類型的對象的位置有序集合,它是可以改變的。
? ?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