在 Python 中的列表中查找某个值的第一个和最后一个索引

编程入门 行业动态 更新时间:2024-10-25 06:25:20
本文介绍了在 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 中的列表中查找某个值的第一个和最后一个索引

本文发布于:2023-10-24 08:36:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523459.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一个   索引   列表中   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!