Python 列表項索引查詢



列表類的 index() 方法返回給定項第一次出現的索引。

語法

list.index(obj)

返回值

index() 方法返回一個整數,表示物件第一次出現的索引。

示例

請看以下示例 -

lst = [25, 12, 10, -21, 10, 100]
print ("lst:", lst)
x = lst.index(10)
print ("First index of 10:", x)

它將產生以下輸出 -

lst: [25, 12, 10, -21, 10, 100]
First index of 10: 2
python_list_methods.htm
廣告