Python入門教程(十二)Python列表
Python 集合(數(shù)組)
Python 編程語言中有四種集合數(shù)據(jù)類型:
- 列表(List)是一種有序和可更改的集合。允許重復的成員。
- 元組(Tuple)是一種有序且不可更改的集合。允許重復的成員。
- 集合(Set)是一個無序和無索引的集合。沒有重復的成員。
- 詞典(Dictionary)是一個無序,可變和有索引的集合。沒有重復的成員。
選擇集合類型時,了解該類型的屬性很有用。
為特定數(shù)據(jù)集選擇正確的類型可能意味著保留含義,并且可能意味著提高效率或安全性。
列表
列表是一個有序且可更改的集合。在 Python 中,列表用方括號編寫。
實例
創(chuàng)建列表:
thislist = ["apple", "banana", "cherry"] print(thislist)
運行實例
訪問項目
您可以通過引用索引號來訪問列表項:
實例
打印列表的第二項:
thislist = ["apple", "banana", "cherry"] print(thislist[1])
運行實例
負的索引
負索引表示從末尾開始,-1 表示最后一個項目,-2 表示倒數(shù)第二個項目,依此類推。
實例
打印列表的最后一項:
thislist = ["apple", "banana", "cherry"] print(thislist[-1])
運行實例
索引范圍
您可以通過指定范圍的起點和終點來指定索引范圍。
指定范圍后,返回值將是包含指定項目的新列表。
實例
返回第三、第四、第五項:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(thislist[2:5])
運行實例
注釋:搜索將從索引 2(包括)開始,到索引 5(不包括)結(jié)束。
請記住,第一項的索引為 0。
負索引的范圍
如果要從列表末尾開始搜索,請指定負索引:
實例
此例將返回從索引 -4(包括)到索引 -1(排除)的項目:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(thislist[-4:-1])
運行實例
更改項目值
如需更改特定項目的值,請引用索引號:
實例
更改第二項:
thislist = ["apple", "banana", "cherry"] thislist[1] = "mango" print(thislist)
運行實例
遍歷列表
您可以使用 for 循環(huán)遍歷列表項:
實例
逐個打印列表中的所有項目:
thislist = ["apple", "banana", "cherry"] for x in thislist: print(x)
運行實例
我們將在 Python For 循環(huán) 這一章中學習有關(guān) for 循環(huán)的更多知識。
檢查項目是否存在
如需確定列表中是否存在指定的項,請使用 in 關(guān)鍵字:
實例
檢查列表中是否存在 “apple”:
thislist = ["apple", "banana", "cherry"] if "apple" in thislist: print("Yes, 'apple' is in the fruits list")
運行實例
列表長度
如需確定列表中有多少項,請使用 len() 方法:
實例
打印列表中的項目數(shù):
thislist = ["apple", "banana", "cherry"] print(len(thislist))
運行實例
添加項目
如需將項目添加到列表的末尾,請使用 append() 方法:
實例
使用 append() 方法追加項目:
thislist = ["apple", "banana", "cherry"] thislist.append("orange") print(thislist)
運行實例
要在指定的索引處添加項目,請使用 insert() 方法:
實例
插入項目作為第二個位置:
thislist = ["apple", "banana", "cherry"] thislist.insert(1, "orange") print(thislist)
運行實例
刪除項目
有幾種方法可以從列表中刪除項目:
實例 remove() 方法刪除指定的項目:
thislist = ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist)
運行實例
實例 pop() 方法刪除指定的索引(如果未指定索引,則刪除最后一項):
thislist = ["apple", "banana", "cherry"] thislist.pop() print(thislist)
運行實例
實例
del 關(guān)鍵字刪除指定的索引:
thislist = ["apple", "banana", "cherry"] del thislist[0] print(thislist)
運行實例
實例
del 關(guān)鍵字也能完整地刪除列表:
thislist = ["apple", "banana", "cherry"] del thislist
運行實例
實例
clear() 方法清空列表:
thislist = ["apple", "banana", "cherry"] thislist.clear() print(thislist)
運行實例
復制列表
您只能通過鍵入 list2 = list1 來復制列表,因為:list2 將只是對 list1 的引用,list1 中所做的更改也將自動在 list2 中進行。
有一些方法可以進行復制,一種方法是使用內(nèi)置的 List 方法 copy()。
實例
使用 copy() 方法來復制列表:
thislist = ["apple", "banana", "cherry"] mylist = thislist.copy() print(mylist)
運行實例
制作副本的另一種方法是使用內(nèi)建的方法 list()。
實例
使用 list() 方法復制列表:
thislist = ["apple", "banana", "cherry"] mylist = list(thislist) print(mylist)
運行實例
合并兩個列表
在 Python 中,有幾種方法可以連接或串聯(lián)兩個或多個列表。
最簡單的方法之一是使用 + 運算符。
實例
合并兩個列表:
list1 = ["a", "b" , "c"] list2 = [1, 2, 3] list3 = list1 + list2 print(list3)
運行實例
連接兩個列表的另一種方法是將 list2 中的所有項一個接一個地追加到 list1 中:
實例
把 list2 追加到 list1 中:
list1 = ["a", "b" , "c"] list2 = [1, 2, 3] for x in list2: list1.append(x) print(list1)
運行實例
或者,您可以使用 extend() 方法,其目的是將一個列表中的元素添加到另一列表中:
實例
使用 extend() 方法將 list2 添加到 list1 的末尾:
list1 = ["a", "b" , "c"] list2 = [1, 2, 3] list1.extend(list2) print(list1)
運行實例
list() 構(gòu)造函數(shù)
也可以使用 list() 構(gòu)造函數(shù)創(chuàng)建一個新列表。
實例
使用 list() 構(gòu)造函數(shù)創(chuàng)建列表:
thislist = list(("apple", "banana", "cherry")) # 請注意雙括號 print(thislist)
運行實例
列表方法
Python 有一組可以在列表上使用的內(nèi)建方法。
到此這篇關(guān)于Python入門教程(十二)Python列表的文章就介紹到這了,更多相關(guān)Python列表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)讀取Linux系統(tǒng)的CPU以及內(nèi)存占用
這篇文章主要為大家詳細介紹了如何利用Python語言實現(xiàn)Linux系統(tǒng)的CPU以及內(nèi)存占用,文中的示例代碼講解詳細,具有一定的學習價值,需要的可以收藏一下2023-05-05在ubuntu16.04中將python3設置為默認的命令寫法
這篇文章主要介紹了在ubuntu16.04中將python3設置為默認python的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-10-10Pandas中字符串和時間轉(zhuǎn)換與格式化的實現(xiàn)
本文主要介紹了Pandas中字符串和時間轉(zhuǎn)換與格式化的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01