1. 程式人生 > >(Python基礎教程之八)Python中的list操作

(Python基礎教程之八)Python中的list操作

1. [Python基礎教程](https://zthinker.com/archives/python基礎教程) 2. [在SublimeEditor中配置Python環境](https://zthinker.com/archives/在sublimeeditor中配置python環境) 3. [Python程式碼中添加註釋](https://zthinker.com/archives/python程式碼中添加註釋) 4. [Python中的變數的使用](https://zthinker.com/archives/python中的變數的使用) 5. [Python中的資料型別](https://zthinker.com/archives/python中的資料型別) 6. [Python中的關鍵字](https://zthinker.com/archives/python中的關鍵字) 7. [Python字串操作](https://zthinker.com/archives/python字串操作) 8. [Python中的list操作](https://zthinker.com/archives/python中的list操作) 9. [Python中的Tuple操作](https://zthinker.com/archives/python中的tuple操作) 10. [Pythonmax()和min()–在列表或陣列中查詢最大值和最小值](https://zthinker.com/archives/pythonmax和min在列表或陣列中查詢最大值和最小值) 11. [Python找到最大的N個(前N個)或最小的N個專案](https://zthinker.com/archives/python找到最大的n個前n個或最小的n個專案) 12. [Python讀寫CSV檔案](https://zthinker.com/archives/python讀寫csv檔案) 13. [Python中使用httplib2–HTTPGET和POST示例](https://zthinker.com/archives/python中使用httplib2httpget和post示例) 14. [Python將tuple開箱為變數或引數](https://zthinker.com/archives/python將tuple開箱為變數或引數) 15. [Python開箱Tuple–太多值無法解壓](https://zthinker.com/archives/python開箱tuple太多值無法解壓) 16. [Pythonmultidict示例–將單個鍵對映到字典中的多個值](https://zthinker.com/archives/pythonmultidict示例將單個鍵對映到字典中的多個值) 17. [PythonOrderedDict–有序字典](https://zthinker.com/archives/pythonordereddict有序字典) 18. [Python字典交集–比較兩個字典](https://zthinker.com/archives/python字典交集比較兩個字典) 19. [Python優先順序佇列示例](https://zthinker.com/archives/python優先順序佇列示例) 在[Python中](https://zthinker.com/archives/python基礎教程),列表為: * 有序 * 索引(索引從0開始) * 易變的 * 異構的(列表中的專案不必是同一型別) * 寫為方括號之間的逗號分隔值列表 ``` listOfSubjects = ['physics', 'chemistry', "mathematics"] listOfIds = [0, 1, 2, 3, 4] miscList = [0, 'one', 2, 'three'] ``` 1\. Access list items --------------------- 要訪問列表中的值,請使用切片語法或陣列索引形式的方括號來獲取單個專案或專案範圍。 傳遞的索引值可以是正數或負數。如果索引是負數則從列表的末尾開始計數。 `list [m : n]`表示子列表從索引`m`(包括)開始,到索引`n`(不包括)結束。 * 如果`m`未提供,則假定其值為零。 * 如果`n`未提供,則選擇範圍直到列表的最後。 ``` ids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print( ids[0] ) # 0 print( ids[1:5] ) # [1, 2, 3, 4] print( ids[ : 3] ) # [0, 1, 2] print( ids[7 : ] ) # [7, 8, 9]\ print( ids[-8:-5] ) # [2, 3, 4] ``` 2\. Modily list --------------- 要更改列表中的特定專案,請使用其索引引用該專案並分配一個新值。 ``` charList = ["a", "b", "c"] charList [2] = "d" print (charList) # ['a', 'b', 'd'] ``` 3\. Iterate a list ------------------ 我們可以使用來遍歷列表項`for loop`。 ``` charList = ["a", "b", "c"] for x in charList: print(x) # a # b # c ``` 4\. Check if a item exists in the list -------------------------------------- 使用`'in'`關鍵字確定列表中是否存在指定的專案。 ``` charList = ["a", "b", "c"] if "a" in charList: print("a is present") # a is present if "d" in charList: print("d is present") else: print("d is NOT present") # d is NOT present ``` 5\. Finding length of the list ------------------------------ 使用該`len()`函式查詢給定列表的長度。 ``` charList = ["a", "b", "c"] x = len (charList) print (x) # 3 ``` 6\. Adding items ---------------- * 要將專案新增到列表的末尾,請使用`append(item)`方法。 * 要在特定索引位置新增專案,請使用`insert(index, item)`方法。如果`index`大於索引長度,則將專案新增到列表的末尾。 ``` charList = ["a", "b", "c"] charList.append("d") charList.append("e") print (charList) # ['a', 'b', 'c', 'd', 'e'] charList.insert(5, "f") print (charList) # ['a', 'b', 'c', 'd', 'e', 'f'] charList.insert(10, "h") # No error print (charList) # ['a', 'b', 'c', 'd', 'e', 'f', 'h'] ``` 7\. Removing items ------------------ 若要從列表中刪除專案,四個途徑使用一個,即`remove()`,`pop()`,`clear()`或`del`關鍵字。 #### 7.1. remove() 它通過其值刪除指定的專案。 ``` charList = ["a", "b", "c"] charList.remove("c") print (charList) # ['a', 'b'] ``` #### 7.2. pop() 它通過索引刪除指定的專案。如果未提供index,它將從列表中刪除最後一項。 ``` charList = ["a", "b", "c", "d"] charList.pop() # removes 'd' - last item print (charList) # ['a', 'b', 'c'] charList.pop(1) # removes 'b' print (charList) # ['a', 'c'] ``` #### 7.3. clear() 它清空列表。 ``` charList = ["a", "b", "c", "d"] charList.clear() print (charList) # [] ``` #### 7.4. del keyword 它可以用來**從列表的索引中刪除專案**。我們也可以使用它**刪除整個列表**。 ``` charList = ["a", "b", "c", "d"] del charList[0] print (charList) # ['b', 'c', 'd'] del charList print (charList) # NameError: name 'charList' is not defined ``` 8\. Join two lists ------------------ 我們可以使用`"+"`運算子或`extend()`函式將兩個給定的列表加入Python 。 ``` charList = ["a", "b", "c"] numList = [1, 2, 3] list1 = charList + numList print (list1) # ['a', 'b', 'c', 1, 2, 3] charList.extend(numList) print (charList) # ['a', 'b', 'c', 1, 2, 3] ``` 9\. Python list methods ----------------------- #### 9.1. append() 在列表的末尾新增一個元素。 ``` charList = ["a", "b", "c"] charList.append("d") print (charList) # ["a", "b", "c", "d"] ``` #### 9.2. clear() 從列表中刪除所有元素。 ``` charList = ["a", "b", "c"] charList.clear() print (charList) # [] ``` #### 9.3. copy() 返回列表的副本。 ``` charList = ["a", "b", "c"] newList = charList.copy() print (newList) # ["a", "b", "c"] ``` #### 9.4. count() 返回具有指定值的元素數。 ``` charList = ["a", "b", "c"] x = charList.count('a') print (x) # 1 ``` #### 9.5. extend() 將列表的元素新增到當前列表的末尾。 ``` charList = ["a", "b", "c"] numList = [1, 2, 3] charList.extend(numList) print (charList) # ['a', 'b', 'c', 1, 2, 3] ``` #### 9.6. index() 返回具有指定值的第一個元素的索引。 ``` charList = ["a", "b", "c"] x = charList.index('a') print (x) # 0 ``` #### 9.7. insert() 在指定位置新增元素。 ``` charList = ["a", "b", "c"] charList.insert(3, 'd') print (charList) # ['a', 'b', 'c', 'd'] ``` #### 9.8. pop() 刪除指定位置或列表末尾的元素。 ``` charList = ["a", "b", "c", "d"] charList.pop() # removes 'd' - last item print (charList) # ['a', 'b', 'c'] charList.pop(1) # removes 'b' print (charList) # ['a', 'c'] ``` #### 9.9. remove() 刪除具有指定值的專案。 ``` charList = ["a", "b", "c", "d"] charList.remove('d') print (charList) # ['a', 'b', 'c'] ``` #### 9.10. reverse() 顛倒列表中專案的順序。 ``` charList = ["a", "b", "c", "d"] charList.reverse() print (charList) # ['d', 'c', 'b', 'a'] ``` #### 9.11. sort() 預設情況下,以升序對給定列表進行排序。 ``` charList = ["a", "c", "b", "d"] charList.sort() print (charList) # ["a", "b", "c", "d"] ``` 學習