Latest web development tutorials

Python List index()方法

Python 列表 Python列表


描述

index() 函數用於從列表中找出某個值第一個匹配項的索引位置。

語法

index()方法語法:

list.index(obj)

參數

  • obj -- 查找的對象。

返回值

該方法返回查找對象的索引位置,如果沒有找到對象則拋出異常。

實例

以下實例展示了index()函數的使用方法:

#!/usr/bin/python

aList = [123, 'xyz', 'zara', 'abc'];

print "Index for xyz : ", aList.index( 'xyz' ) ;
print "Index for zara : ", aList.index( 'zara' ) ;

以上實例輸出結果如下:

Index for xyz :  1
Index for zara :  2

Python 列表 Python列表