Fastest way to check if a value exists in a list

后端 未结 12 2099
猫巷女王i
猫巷女王i 2020-11-22 00:18

What is the fastest way to know if a value exists in a list (a list with millions of values in it) and what its index is?

I know that all values in the list are uniqu

12条回答
  •  面向向阳花
    2020-11-22 00:55

    Be aware that the in operator tests not only equality (==) but also identity (is), the in logic for lists is roughly equivalent to the following (it's actually written in C and not Python though, at least in CPython):

    for element in s:
        if element is target:
            # fast check for identity implies equality
            return True
        if element == target:
            # slower check for actual equality
            return True
    return False
    

    In most circumstances this detail is irrelevant, but in some circumstances it might leave a Python novice surprised, for example, numpy.NAN has the unusual property of being not being equal to itself:

    >>> import numpy
    >>> numpy.NAN == numpy.NAN
    False
    >>> numpy.NAN is numpy.NAN
    True
    >>> numpy.NAN in [numpy.NAN]
    True
    

    To distinguish between these unusual cases you could use any() like:

    >>> lst = [numpy.NAN, 1 , 2]
    >>> any(element == numpy.NAN for element in lst)
    False
    >>> any(element is numpy.NAN for element in lst)
    True 
    

    Note the in logic for lists with any() would be:

    any(element is target or element == target for element in lst)
    

    However, I should emphasize that this is an edge case, and for the vast majority of cases the in operator is highly optimised and exactly what you want of course (either with a list or with a set).

提交回复
热议问题