在 Python 中的列表中查找某个值的第一个和最后一个索引 [英] Finding first and last index of some value in a list in Python

查看:39
本文介绍了在 Python 中的列表中查找某个值的第一个和最后一个索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何作为列表一部分的内置方法可以为我提供某个值的第一个和最后一个索引,例如:

Is there any built-in methods that are part of lists that would give me the first and last index of some value, like:

verts.IndexOf(12.345)
verts.LastIndexOf(12.345)

推荐答案

Sequences 有一个方法 index(value) 返回第一次出现的索引 - 在你的情况下这将是 verts.索引(值).

Sequences have a method index(value) which returns index of first occurrence - in your case this would be verts.index(value).

您可以在 verts[::-1] 上运行它以找出最后一个索引.在这里,这将是 len(verts) - 1 - verts[::-1].index(value)

You can run it on verts[::-1] to find out the last index. Here, this would be len(verts) - 1 - verts[::-1].index(value)

这篇关于在 Python 中的列表中查找某个值的第一个和最后一个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆