2014-04-01 27 views
1

在蟒蛇,如果我有一個名單說[2,1,1,3,5]末,則有可能得到Python的list.index()從列表中

[2,1,1,3,5].index(1)作爲2即第一場比賽從高端而不是下開始?

+0

的可能重複的[Equivelant到RINDEX用於列出在Python](http://stackoverflow.com/questions/9836425/equivelant-to-rindex-for-lists-in-python) – alecxe

+0

呀,它的副本http://stackoverflow.com/questions/6890170/python-how-to-find-last-occurrence-in-a-list-in-python,有人可以關閉它嗎? – user80551

回答

1

不能說我曾經需要做到這一點,但你總是有本事:

lst = [2,1,1] 
reverseindex = len(lst)-1 - lst[::-1].index(1) 

請注意,如果你有一個字符串,你可以這樣做:

string = "21135" 
reverseindex = string.rindex(1) 
# reverseindex == 2 

但是列表沒有這個功能。