跳到主要内容

Python List index()方法


描述

index() 函数用于从列表中找出某个值第一个匹配项的索引位置。

语法

index()方法语法:

示例(Python 2.0+)
list.index(x[, start[, end]])

参数

  • x-- 查找的对象。
  • start-- 可选,查找的起始位置。
  • end-- 可选,查找的结束位置。

返回值

该方法返回查找对象的索引位置,如果没有找到对象则抛出异常。

示例

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

示例(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-

aList = [123, 'xyz', 'lectcode', 'abc']

print "xyz 索引位置: ", aList.index( 'xyz' )
print "lectcode 索引位置 : ", aList.index( 'lectcode', 1, 3 )

以上实例输出结果如下:

xyz 索引位置:  1
lectcode 索引位置 : 2