Python3 list.index()方法

index()方法返回obj是否出现列表中的最低索引。

语法

以下是 index()方法的语法 -
list.index(obj)

参数

  • obj -- 这是要查找的对象。

返回值

此方法返回找到的对象的索引,如果指定值没有找到,则抛出一个异常。

示例

下面的示例显示index() 方法的使用。
#!/usr/bin/python3

list1 = ['physics', 'chemistry', 'maths']
print ('Index of chemistry', list1.index('chemistry'))
print ('Index of C#', list1.index('C#'))
 
当我们运行上面的程序,会产生以下结果 -
Traceback (most recent call last):
  File "test.py", line 3, in 
    print ('Index of C#', list1.index('C#'))
ValueError: 'C#' is not in list

上一篇: Python3字符串 下一篇: Python3列表