本文介绍了在 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 中的列表中查找某个值的第一个和最后一个索引
发布评论