列表末尾的Python list.index() [英] Python list.index() from end of the list
本文介绍了列表末尾的Python list.index()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在python中,如果我有一个列表说 [2,1,1,3,5]
,那么有可能获得
In python, if I have a list say [2,1,1,3,5]
, then is it possible to get
[2,1,1,3,5] .index(1)
作为 2
,即从高端开始而不是从低端开始的第一个匹配?>
[2,1,1,3,5].index(1)
as 2
i.e first match starting from the higher end instead of the lower?
推荐答案
不能说我曾经需要这样做,但是您始终可以使用以下方法来破解它:
Can't say I've ever needed to do this, but you could always hack it with:
lst = [2,1,1]
reverseindex = len(lst)-1 - lst[::-1].index(1)
请注意,如果您有STRING,则可以执行以下操作:
Note that if you had a STRING, you could do:
string = "21135"
reverseindex = string.rindex(1)
# reverseindex == 2
但是列表不具有此功能.
But lists don't have this function.
这篇关于列表末尾的Python list.index()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文