Python List index()方法
描述
index() 函數用於從列表中找出某個值第一個匹配項的索引位置。
語法
index()方法語法:
list.index(x[, start[, end]])
參數
- x-- 查找的對象。
- start-- 可選,查找的起始位置。
- end-- 可選,查找的結束位置。
返回值
該方法返回查找對象的索引位置,如果沒有找到對象則拋出異常。
實例
以下實例展示了 index()函數的使用方法:
實例
#!/usr/bin/python
# -*- coding: UTF-8 -*-
aList = [123, 'xyz', 'zaixian', 'abc']
print "xyz 索引位置: ", aList.index( 'xyz' )
print "zaixian 索引位置 : ", aList.index( 'zaixian', 1, 3 )
# -*- coding: UTF-8 -*-
aList = [123, 'xyz', 'zaixian', 'abc']
print "xyz 索引位置: ", aList.index( 'xyz' )
print "zaixian 索引位置 : ", aList.index( 'zaixian', 1, 3 )
以上實例輸出結果如下:
xyz 索引位置: 1 zaixian 索引位置 : 2