Python 列表

Python 集合(數組)

Python 編程語言中有四種集合數據類型:

  • 列表(List)是一種有序和可更改的集合。允許重復的成員。
  • 元組(Tuple)是一種有序且不可更改的集合。允許重復的成員。
  • 集合(Set)是一個無序和無索引的集合。沒有重復的成員。
  • 詞典(Dictionary)是一個無序,可變和有索引的集合。沒有重復的成員。

選擇集合類型時,了解該類型的屬性很有用。

為特定數據集選擇正確的類型可能意味著保留含義,并且可能意味著提高效率或安全性。

列表

列表是一個有序且可更改的集合。在 Python 中,列表用方括號編寫。

實例

創建列表:

thislist = ["apple", "banana", "cherry"]
print(thislist)

運行實例

訪問項目

您可以通過引用索引號來訪問列表項:

實例

打印列表的第二項:

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

運行實例

負的索引

負索引表示從末尾開始,-1 表示最后一個項目,-2 表示倒數第二個項目,依此類推。

實例

打印列表的最后一項:

thislist = ["apple", "banana", "cherry"]
print(thislist[-1])

運行實例

索引范圍

您可以通過指定范圍的起點和終點來指定索引范圍。

指定范圍后,返回值將是包含指定項目的新列表。

實例

返回第三、第四、第五項:

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])

運行實例

注釋:搜索將從索引 2(包括)開始,到索引 5(不包括)結束。

請記住,第一項的索引為 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 循環遍歷列表項:

實例

逐個打印列表中的所有項目:

thislist = ["apple", "banana", "cherry"]
for x in thislist:
  print(x)

運行實例

您將在 Python For 循環 這一章中學習有關 for 循環的更多知識。

檢查項目是否存在

如需確定列表中是否存在指定的項,請使用 in 關鍵字:

實例

檢查列表中是否存在 “apple”:

thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
  print("Yes, 'apple' is in the fruits list")

運行實例

列表長度

如需確定列表中有多少項,請使用 len() 方法:

實例

打印列表中的項目數:

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 關鍵字刪除指定的索引:

thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)

運行實例

實例

del 關鍵字也能完整地刪除列表:

thislist = ["apple", "banana", "cherry"]
del thislist

運行實例

實例

clear() 方法清空列表:

thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)

運行實例

復制列表

您只能通過鍵入 list2 = list1 來復制列表,因為:list2 將只是對 list1 的引用,list1 中所做的更改也將自動在 list2 中進行。

有一些方法可以進行復制,一種方法是使用內置的 List 方法 copy()

實例

使用 copy() 方法來復制列表:

thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)

運行實例

制作副本的另一種方法是使用內建的方法 list()

實例

使用 list() 方法復制列表:

thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)

運行實例

合并兩個列表

在 Python 中,有幾種方法可以連接或串聯兩個或多個列表。

最簡單的方法之一是使用 + 運算符。

實例

合并兩個列表:

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() 構造函數

也可以使用 list() 構造函數創建一個新列表。

實例

使用 list() 構造函數創建列表:

thislist = list(("apple", "banana", "cherry")) # 請注意雙括號
print(thislist)

運行實例

列表方法

Python 有一組可以在列表上使用的內建方法。

方法 描述
append() 在列表的末尾添加一個元素
clear() 刪除列表中的所有元素
copy() 返回列表的副本
count() 返回具有指定值的元素數量。
extend() 將列表元素(或任何可迭代的元素)添加到當前列表的末尾
index() 返回具有指定值的第一個元素的索引
insert() 在指定位置添加元素
pop() 刪除指定位置的元素
remove() 刪除具有指定值的項目
reverse() 顛倒列表的順序
sort() 對列表進行排序