方法 | 描述 |
---|---|
list.index(x[, i[, j]]) | 回傳 x 在 list 最小的索引值 |
舉例示範如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | a = [ "a" , 1 , "hello" , 0 ] print (a.index( 1 )) a.extend([ 0 , 1 ]) print () print (a.index( 1 , 5 )) a.extend([ 0 , 1 ]) print () print (a.index( 1 , 5 , 9 )) a.extend([ 0 , 1 ]) print () print (a.index( 1 , 7 , 13 )) a.extend([ 0 , 1 ]) print () print (a.index( 1 )) # 《程式語言教學誌》的範例程式 # 檔名:listindex.py # 功能:示範 Python 程式 # 作者:張凱慶 # 時間:西元 2010 年 12 月 |
執行結果如下

中英文術語對照 | |
---|---|
串列 | list |
型態 | type |
方法 | method |
1 則留言:
你這樣打很容易模糊焦點
list.index(x[, i[, j]])
這語法講的是我要從一個list當中找一個排序最近(index)的數值或字串x
從第i個搜尋到第j個結束
以你上面的例子來說
ex.
print(a.index(1, 5, 9))
a.extend([0, 1])
print()
由於再更上面的語法輸入了a.extend[0,1]兩次
代表我將a這個list擴充了兩組[0,1]
所以此時的a=["a", 1, "hello", 0, 0, 1, 0, 1]
0 1 2 3 4 5 6 7
我們要尋找從序列5到序列9當中最近的數字1
(然後很靠杯的是你a的長度只有到7還故意打5到9誤導別人幹嘛?)
所以此時就找到最近的數字1是在序列5的地方
print(a.index(1, 5, 9))之後才會顯示5
代表在a這個列表裡面序列5到序列9當中
排序最近的1是在序列5的位子
張貼留言