Python 列表
阿新 • • 發佈:2017-06-30
() line clas python error isp [] 可變 play
Python 列表
1、列表是任意對象的有序集合:數字、字符串、其他列表
2、列表可通過偏移讀取,可改變長度、異構以及任意嵌套
3、屬於可變序列的分類——支持在原處修改
4、對象引用數組
In [1]: L=[] In [2]: print(L) [] In [3]: L=[0,1,2,3,4] In [4]: print(L,L[3]) File "<ipython-input-4-8880841679be>", line 1 print(L,L[3]) ^ SyntaxError: invalid characterView Codein identifier In [5]: print(L,L[3]) [0, 1, 2, 3, 4] 3 In [6]: L=[0,1,[2,3],4] In [7]: print(L) [0, 1, [2, 3], 4] In [8]: L=list(‘SPAM‘) In [9]: print(L) [‘S‘, ‘P‘, ‘A‘, ‘M‘] In [10]: L=list(range(-4,4)) In [11]: print(L) [-4, -3, -2, -1, 0, 1, 2, 3] In [12]: L*3 Out[12]: [-4, -3, -2, -1, 0, 1, 2, 3, -4, -3, -2, -1, 0, 1, 2, 3, -4, -3, -2, -1, 0, 1, 2, 3] In [13]: print(L.index(1)) 5 In [14]: print(3 in L) True In [15]: for x in L: ...: print(x) ...: -4 -3 -2 -1 0 1 2 3 In [16]: L.pop() Out[16]: 3 In [17]: print(L) [-4, -3, -2, -1, 0, 1, 2] In [18]: L.remove(2) In [19]: print(L) [-4, -3, -2, -1, 0, 1] In [20]: L.remove(0) In [21]: print(L) [-4, -3, -2, -1, 1]
5、矩陣用來表示列表中的嵌套結構,一次索引得到一整行,二次索引得到一項 martix=[[1,2,3],[4,5,6],[7,8,9]] martix[0] martix[2][2]
6、對於列表,索引和切片的賦值都是原地修改,而不是生成新的列表
7、列表方法調用
.append() 末尾加項
.sort() 排序
Python 列表