python list

同屬於一個列表的數據,可以是不同的類型
特色:存儲於用一個列表的數據都是以數字來作爲索引的,即作爲操作存取其中各個元素的依據。
a_list
0 1 2 3 4
int int int int int
1 3 5 7 9
索引分別爲 0,1,2,3,4
每個元素可有自已的類型,均爲int,內容分別是
1、3、5、7、9
a_list = [ 1,3,5,7,9 ]
數字列表
\>>> a_list=[1,3,5,7,9]
\>>> a_list
[1, 3, 5, 7, 9]
\>>> a_list[0]
1
字符串列表
\>>> str_list=['P','y','t','h','o','n']
\>>> str_list
['P', 'y', 't', 'h', 'o', 'n']
\>>> str_list[2]
't'
字符串split 方法
\>>> str_msg="I Love Pyton"
\>>> b_list=str_msg.split()
\>>> b_list
['I', 'Love', 'Pyton']
一個英文句子拆成字母所組成的列表,用list() 函數,
\>>> str_msg="I Love Pyton"
\>>> c_list=list(str_msg)
\>>> c_list
['I', ' ', 'L', 'o', 'v', 'e', ' ', 'P', 'y', 't', 'o', 'n']
\>>>
同一個列表中可以用不同的數據類型,列表中也可以有其他的列表
\>>> k1=['book',10]
\>>> k2=['campus',15]
\>>> k3=['cook',9]
\>>> k4=['Python',26]
\>>> keywords=[k1,k2,k3,k4]
\>>> keywords
[['book', 10], ['campus', 15], ['cook', 9], ['Python', 26]]
\>>> keywords[2]
['cook', 9]
\>>> keywords[2][0]
'cook'
\>>> keywords[2][1]
9
\>>>
可以使用”+“運算把兩個列表放在一起,還可以 檢測某一個數據是否在列表之中
\>>> "Python" in k4
True
\>>> k4 in keywords
True
\>>> ["Python",26] in keywords
True
\>>> keywords+k1+k2
[['book', 10], ['campus', 15], ['cook', 9], ['Python', 26], 'book', 10, 'campus', 15]
\>>>

比較常用的操作方法和函數

列表表達式 操作結果說明
lst * n 把lst類表重複n次
lst[n1:n2] 把索引組n1到n2的列表內容取出,組成一個列表
lst[n1:n2:k] 同上,但取出間隔爲k
del lst[n1:n2] 刪除索引值n1到n2之間的元素
lst[n1:n2]=n 把索引值n1到n2之間的元素設置爲n
lst[n1:n2:k]=n 同上,但間隔爲k
del lst[n1:n2:k] 刪除索引值n1到n2之間的元素,但間隔爲k
len(lst) 放回類表的個數
min(lst) 返回列表的最小值
max(lst) 返回列表的最大值
sum(lst) 返回列表的求和值
lst.index(n) 返回列表中第一個出現n的索引值
lst.count(n) 計算出n 在列表中出現的次數

\>>> x=list(range(10))
\>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
\>>> x*2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
\>>> y=x[1:7]
\>>> y
[1, 2, 3, 4, 5, 6]
\>>> y=x[1:7:2]
\>>> y
[1, 3, 5]
\>>> del x[1:7]
\>>> x
[0, 7, 8, 9]
\>>> x=list(range(10))
\>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
\>>> del x[1:7:2]
\>>> x
[0, 2, 4, 6, 7, 8, 9]
\>>> x=list(range(10))
\>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
\>>> len(x)
10
\>>> max(x)
9
\>>> min(x)
0
\>>> x=list(range(3))
\>>> x
[0, 1, 2]
\>>> sum(x)
3
\>>> msg="Here is the string"
\>>> lst=list(msg)
\>>> lst
['H', 'e', 'r', 'e', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 's', 't', 'r', 'i', 'n', 'g']
\>>> lst.index('e')
1
\>>> lst.count('e')
3
\>>>

列表操作方法:
(其中lst表示列表變量,x表示列表元素或是另外一個列表變量,n爲數值)
lst.append(x) 將x視爲一個元素,附加到列表的後面
lst.extend(x) 將x 中的所有元素附加到列表的後面
lst.insert(n,x) 把x插入到索引值爲n的地方
lst.pop() 彈出列表中最後一個元素,可以參數指定特定的索引
lst.remove(x) 從列表中刪除第一個出現的x
lst.reverse() 反轉列表的順序
lst.sort() 將列表的元素內容加以排序

append 與 extend 區別
\>>> lsta=[1,2,3,4,5]
\>>> exb=[5,5,5,5]
\>>> lsta.extend(exb)
\>>> lsta
[1, 2, 3, 4, 5, 5, 5, 5, 5]
\>>> lsta=[1,2,3,4,5]
\>>> lsta.append(exb)
\>>> lsta
[1, 2, 3, 4, 5, [5, 5, 5, 5]]
pop操作
\>>> lst=[0,1,2,3,4,5]
\>>> lst.append(9)
\>>> lst.append('x')
\>>> lst
[0, 1, 2, 3, 4, 5, 9, 'x']
\>>> lst.pop()
'x'
\>>> lst.pop(2)
2
\>>> lst
[0, 1, 3, 4, 5, 9]
\>>>
\>>> cars=['bwm','audi','toyota','subaru']
\>>> cars.sort()
\>>> cars
['audi', 'bwm', 'subaru', 'toyota']
\>>> cars.reverse()
\>>> cars
['toyota', 'subaru', 'bwm', 'audi']
\>>>
\>>> cars.remove('bwm') 用於不知道具體的索引位置,只知道具體的值
\>>> cars
['toyota', 'subaru', 'audi']
\>>> cars.insert(1,'haima') 指定位置插入
\>>> cars
['toyota', 'haima', 'subaru', 'audi']
\>>>

\>>> cars
['toyota', 'haima', 'subaru', 'audi']
\>>> for x in cars:
... print x
...
toyota
haima
subaru
audi
\>>> for x in cars:
... print x.title()
...
Toyota
Haima
Subaru
Audi
\>>>
通過循環創建所需列表
\>>> atest=[]
\>>> for value in range(1,11,2):
... a=value**2
... atest.append(a)
...
\>>> print atest
[1, 9, 25, 49, 81]

\>>> atest2=[value**2 for value in range(1,11,2)]
\>>> print atest2
[1, 9, 25, 49, 81]
\>>>

\>>> list_2d=[[0 for i in range(5)]for i in range(5)]
\>>> list_2d
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
\>>> list_2d[0].append(3)
\>>> list_2d[2].append(7)
\>>> list_2d
[[0, 0, 0, 0, 0, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 7], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
\>>>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章