美文网首页
python的index函数,获取最后一个

python的index函数,获取最后一个

作者: Ribosome_He | 来源:发表于2021-10-12 11:17 被阅读0次

python的index函数可以获取列表中值的第一个索引。

list= [1,2,3,4,5,1,2,2]

list.index(2) >>>>>>>> 1

如果要获取相同值的最后一个索引:
len(list) - list[::-1].index(2) - 1
反向取得list后,用list的长度减去反转后出现的第一个索引再减1

相关文章

网友评论

      本文标题:python的index函数,获取最后一个

      本文链接:https://www.haomeiwen.com/subject/myydoltx.html