清單

回報問題 查看來源 Nightly · 8.4 · 8.3 · 8.2 · 8.1 · 8.0 · 7.6

內建清單型別。清單運算式範例:

x = [1, 2, 3]
您可以使用索引 (從 0 開始) 存取元素:
e = x[1]   # e == 2
清單支援 + 運算子,可串連兩個清單。範例:
x = [1, 2] + [3, 4]   # x == [1, 2, 3, 4]
x = ["a", "b"]
x += ["c"]            # x == ["a", "b", "c"]
與字串類似,清單支援切片作業:
['a', 'b', 'c', 'd'][1:3]   # ['b', 'c']
['a', 'b', 'c', 'd'][::2]  # ['a', 'c']
['a', 'b', 'c', 'd'][3:0:-1]  # ['d', 'c', 'b']
清單是可變動的,如同 Python。

成員

附加

None list.append(item)

Adds an item to the end of the list.

參數

參數 說明
item 必要
要新增至結尾的項目。

關閉

None list.clear()

移除清單的所有元素。

延長

None list.extend(items)

將所有項目加到清單結尾。

參數

參數 說明
items 必要
要在結尾新增的項目。

索引

int list.index(x, start=None, end=None)

傳回清單中第一個值為 x 的項目的索引。如果沒有這類項目,就會發生錯誤。

參數

參數 說明
x 必要
要搜尋的物件。
start int;或 None; 預設值為 None
要檢查的清單部分的起始索引。
end int;或 None; 預設值為 None
要檢查的清單部分結尾索引。

insert

None list.insert(index, item)

在指定位置插入項目。

參數

參數 說明
index int; required
指定位置的索引。
item 必要
項目。

流行

unknown list.pop(i=-1)

移除清單中指定位置的項目,並傳回該項目。如果未指定 index,系統會移除並傳回清單中的最後一個項目。

參數

參數 說明
i int;或 None; 預設值為 -1
項目的索引。

移除

None list.remove(x)

從清單中移除值為 x 的第一個項目。如果沒有這類項目,就會發生錯誤。

參數

參數 說明
x 必要
要移除的物件。