Python 列表 index() 方法

實例

值 "cherry" 的位置是:

fruits = ['apple', 'banana', 'cherry']
x = fruits.index("cherry")

運行實例

定義和用法

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

語法

list.index(element)

參數值

參數 描述
element 必需。任何類型(字符串、數字、列表等)。要搜索的值。

更多實例

實例

值 32 的位置是:

fruits = [4, 55, 64, 32, 16, 32]
x = fruits.index(32)

運行實例

注釋:index() 方法僅返回值的首次出現。