Python: finding an element in an array

This question already has an answer here:

  • Finding the index of an item given a list containing it in Python 23 answers

  • 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.

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

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

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

    Source


    从潜入Python:

    >>> li
    ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
    >>> li.index("example")
    5
    

    如果你只是想知道元素是否包含在列表中或者不是:

    >>> li
    ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
    >>> 'example' in li
    True
    >>> 'damn' in li
    False
    
    链接地址: http://www.djcxy.com/p/28104.html

    上一篇: 如何在Pandas DataFrame中的/ in运算符中使用?

    下一篇: Python:在数组中找到一个元素