Python 元組 index() 方法
實例
檢索首次出現的值 8,并返回其位置:
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5) x = thistuple.index(8) print(x)
定義和用法
index() 方法查找指定值的第一次出現。
如果未找到該值,index() 方法將引發異常。
語法
tuple.index(value)
參數值
參數 | 描述 |
---|---|
value | 必需。要檢索的項目。 |
檢索首次出現的值 8,并返回其位置:
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5) x = thistuple.index(8) print(x)
index() 方法查找指定值的第一次出現。
如果未找到該值,index() 方法將引發異常。
tuple.index(value)
參數 | 描述 |
---|---|
value | 必需。要檢索的項目。 |