蟒:以阵列寻找一个元件 [英] Python: finding an element in an array

查看:132
本文介绍了蟒:以阵列寻找一个元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是找到一个元素的Python中数组索引的好办法?
注意,该阵列可以不被排序。
有没有指定要使用的比较操作的方法吗?

What is a good way to find the index of an element in an array in python? Note that the array may not be sorted. Is there a way to specify what comparison operator to use?

推荐答案

最好的办法可能是使用列表方法的.index。

The best way is probably to use the list method .index.

对于列表中的对象,你可以这样做:

For the objects in the list, you can do something like:

def __eq__(self, other):
    return self.Value == other.Value

您所需要的任何特殊处理。

with any special processing you need.

您也可以使用用于/在枚举声明(ARR)

You can also use a for/in statement with enumerate(arr)

发现,具有价值的项目的索引的例子> 100

Example of finding the index of an item that has value > 100.

for index, item in enumerate(arr):
    if item > 100:
        return index, item

来源

这篇关于蟒:以阵列寻找一个元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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