Python 學習日記第二篇 — 列表
- 2020 年 1 月 14 日
- 筆記
一、列表
列表是一個可以包含所有數據類型的對象的位置有序集合,它是可以改變的。
1、列表的序列操作(Python3)
>>> 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、列表的方法
#在列表中加入元素 >>> 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、列表的嵌套
>>> one_list = [2,3,1,7,[2,"gg","david"],87,98] >>> one_list[4][1][1] 'g' >>> one_list[4][2] 'david'
4、列表解析
>>> 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、練習
''' 練習:找列表中的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、元組的序列操作
>>> 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、元組的方法
#查看元素在元組中的偏移量 >>> one_tuple.index(2) 1 #統計元素在元組中出現的次數 >>> one_tuple.count(2) 1
3、元組的嵌套
元組本身的元素是不可被修改的,但元組中嵌套的字典或列表的元素是可變的。
>>> 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])