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

查看:1222
本文介绍了在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)

推荐答案

序列具有方法index(value),该方法返回首次出现的索引-在您的情况下为verts.index(value).

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天全站免登陆