Python 列表 index() 方法

定义和用法

index() 方法返回指定值首次出现的位置。

实例

例子 1

值 "cherry" 的位置是:

fruits = ['apple', 'banana', 'cherry']

x = fruits.index("cherry")

运行实例

例子 2

值 32 的位置是:

fruits = [4, 55, 64, 32, 16, 32]

x = fruits.index(32)

运行实例

注释:index() 方法仅返回值的首次出现。

语法

list.index(element)

参数

参数 描述
element 必需。要搜索的值。任何类型(字符串、数字、列表等)。