7
在Python在開始的數組元素,您可以指定開始和結束搜索的索引時列表元素:搜索一個給定的指標
>>> l = ['a', 'b', 'a']
>>> l.index('a')
0
>>> l.index('a', 1) # begin at index 1
2
>>> l.index('a', 1, 3) # begin at index 1 and stop before index 3
2
>>> l.index('a', 1, 2) # begin at index 1 and stop before index 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'a' is not in list
是否有紅寶石等的功能?你可以使用數組切片,但由於它需要中間對象,看起來好像效率不高。