最近,由于工作需要,使用python開發公司的運維自動化平臺,所以找本書來并結合官方手冊,開始python的學習之旅。
一、列表
【含義】:列表用中括號表示,通過逗號進行分隔一組數據(可以為不同的數據類型),如以下的聲明:
1 >>> language = ['chinese','english','japanese']2 >>> contries = ['China','Amercia','England','Japan']3 4 >>> edward = ['Edward Gumby',42] #不同的數據類型5 >>> john = ['John Smith',50]6 >>> database = [edward,john] #列表可以嵌套
【操作】:訪問、插入、刪除、求最大值最小值及長度等
1、訪問:可以通過索引或分片進行列表的遍歷訪問,索引可以訪問某個位置的值,分片可以靈活的訪問一定范圍的值,如下所示:
1 >>> language = ['chinese','english','japanese'] 2 # 索引訪問 3 >>> language[0] 4 'chinese' 5 6 # 分片訪問 7 # 第一個索引元素包括在分片中,第二個索引不包括在分片中 8 # 可以設置分片的步長 9 >>> language[::]10 ['chinese', 'english', 'japanese']11 >>> language[0:3]12 ['chinese', 'english', 'japanese']13 >>> language[0:2]14 ['chinese', 'english']15 >>> language[1:2]16 ['english']17 # 設置步長為218 >>> num = [1,2,3,4,5,6,7,8,9]19 >>> num[0:9:2]20 [1, 3, 5, 7, 9]
21 >>> num = [1, 2, 3]
22 >>> max(num)
23 3
24 >>> min(num)
25 1
26 >>> len(num)
27 3
2、修改、插入和刪除操作,由此看來列表是可以進行修改的。
1 #修改列表某個元素:索引賦值 2 >>> num = [1,2,3] 3 >>> num[0]=5 #必須為存在的位置索引賦值,否則報錯 4 >>> num 5 [5, 2, 3] 6 7 #修改列表某段范圍的值:分片賦值 8 >>> num[2:]=[6,7,9] #分片賦值元素個數可以不等長 9 >>> num10 [5, 2, 6, 7, 9]11 12 #刪除某個元素13 >>> del num[4]14 >>> num15 [5, 2, 6, 7]
【方法】對于列表,python內置了諸多方法供操作,主要常用的有以下幾個:
1 #append方法:用于在列表最后添加元素,該方法直接修改原列表并返回修改完后的新列表 2 >>> str = ['a','b','c','d'] 3 >>> str.append('e') 4 >>> str 5 ['a', 'b', 'c', 'd', 'e'] 6 7 #count方法:統計某個元素在列表中出現的次數 8 >>> str = ['a','b','c','d'] 9 >>> str.count('a')10 111 12 #extend方法:list.extend(L),L指的是列表對象13 #用另一個列表擴展一個列表,相當于兩個列表連接,14 #但是又不同于連接操作,因為extend方法直接修改原列表并返回,15 #連接操作不影響原有的列表值16 >>> str1 = ['hello,']17 >>> str2 = ['world']18 >>> str1.extend(str2)19 >>> str120 ['hello,', 'world']21 22 >>> str3 = ['a']23 >>> str4 = ['5']24 >>> str3 = str3+str4 #效率沒有extend高25 >>> str326 ['a', '5']27 28 #index方法:返回匹配項的第一個索引位置29 >>> knight = ['we','you','we','me','he']30 >>> knight.index('we')31 032 33 #insert方法:list.insert(i, x)插入x到該i位置34 >>> knight.insert(1,'she')35 >>> knight36 ['we', 'she', 'you', 'we', 'me', 'he']37 38 #remove方法:list.remove(x)39 #刪除列表中為X的第一個出現元素40 >>> knight = ['we', 'she', 'you', 'we', 'me', 'he']41 >>> knight.remove('we')42 >>> knight43 ['she', 'you', 'we', 'me', 'he']44 45 #reverse方法:將元素倒序存儲46 >>> str = ['a', 'b']47 >>> str.reverse()48 >>> str49 ['b', 'a']50 51 #sort方法:list.sort(key=None, reverse=False)52 #直接改變原列表順序,而不是改變副本,對于這種需求如53 #僅僅對副本進行排序,不改變原列表不能直接用sort54 >>> num = [1,3,2,4]55 >>> num.sort()56 >>> num57 [1, 2, 3, 4]58 59 #pop方法:list.pop([i])60 #刪除指定索引位置的元素值,并返回該值61 >>> num = [1, 2, 3, 4]62 >>> num.pop(3)63 4
【常用舉例】:模擬實現堆棧操作和隊列操作
堆棧:后進先出
1 >>> stack = [3, 4, 5] 2 >>> stack.append(6) 3 >>> stack.append(7) 4 >>> stack 5 [3, 4, 5, 6, 7] 6 >>> stack.pop() 7 7 8 >>> stack 9 [3, 4, 5, 6]10 >>> stack.pop()11 612 >>> stack.pop()13 514 >>> stack15 [3, 4]
隊列:先進先出
1 >>> from collections import deque 2 >>> queue = deque(["Eric", "John", "Michael"]) 3 >>> queue.append("Terry") # Terry arrives 4 >>> queue.append("Graham") # Graham arrives 5 >>> queue.popleft() # The first to arrive now leaves 6 'Eric' 7 >>> queue.popleft() # The second to arrive now leaves 8 'John' 9 >>> queue # Remaining queue in order of arrival10 deque(['Michael', 'Terry', 'Graham'])
新聞熱點
疑難解答