How to check if one of the following items is in a list?

前端 未结 14 2045
终归单人心
终归单人心 2020-11-22 17:02

I\'m trying to find a short way to see if any of the following items is in a list, but my first attempt does not work. Besides writing a function to accomplish this, is the

相关标签:
14条回答
  • 2020-11-22 17:50

    Ah, Tobias you beat me to it. I was thinking of this slight variation on your solution:

    >>> a = [1,2,3,4]
    >>> b = [2,7]
    >>> any(x in a for x in b)
    True
    
    0 讨论(0)
  • 2020-11-22 17:50

    In some cases (e.g. unique list elements), set operations can be used.

    >>> a=[2,3,4]
    >>> set(a) - set([2,3]) != set(a)
    True
    >>> 
    

    Or, using set.isdisjoint(),

    >>> not set(a).isdisjoint(set([2,3]))
    True
    >>> not set(a).isdisjoint(set([5,6]))
    False
    >>> 
    
    0 讨论(0)
  • 2020-11-22 17:50

    I collected several of the solutions mentioned in other answers and in comments, then ran a speed test. not set(a).isdisjoint(b) turned out the be the fastest, it also did not slowdown much when the result was False.

    Each of the three runs tests a small sample of the possible configurations of a and b. The times are in microseconds.

    Any with generator and max
            2.093 1.997 7.879
    Any with generator
            0.907 0.692 2.337
    Any with list
            1.294 1.452 2.137
    True in list
            1.219 1.348 2.148
    Set with &
            1.364 1.749 1.412
    Set intersection explcit set(b)
            1.424 1.787 1.517
    Set intersection implicit set(b)
            0.964 1.298 0.976
    Set isdisjoint explicit set(b)
            1.062 1.094 1.241
    Set isdisjoint implicit set(b)
            0.622 0.621 0.753
    

    import timeit
    
    def printtimes(t):
        print '{:.3f}'.format(t/10.0),
    
    setup1 = 'a = range(10); b = range(9,15)'
    setup2 = 'a = range(10); b = range(10)'
    setup3 = 'a = range(10); b = range(10,20)'
    
    print 'Any with generator and max\n\t',
    printtimes(timeit.Timer('any(x in max(a,b,key=len) for x in min(b,a,key=len))',setup=setup1).timeit(10000000))
    printtimes(timeit.Timer('any(x in max(a,b,key=len) for x in min(b,a,key=len))',setup=setup2).timeit(10000000))
    printtimes(timeit.Timer('any(x in max(a,b,key=len) for x in min(b,a,key=len))',setup=setup3).timeit(10000000))
    print
    
    print 'Any with generator\n\t',
    printtimes(timeit.Timer('any(i in a for i in b)',setup=setup1).timeit(10000000))
    printtimes(timeit.Timer('any(i in a for i in b)',setup=setup2).timeit(10000000))
    printtimes(timeit.Timer('any(i in a for i in b)',setup=setup3).timeit(10000000))
    print
    
    print 'Any with list\n\t',
    printtimes(timeit.Timer('any([i in a for i in b])',setup=setup1).timeit(10000000))
    printtimes(timeit.Timer('any([i in a for i in b])',setup=setup2).timeit(10000000))
    printtimes(timeit.Timer('any([i in a for i in b])',setup=setup3).timeit(10000000))
    print
    
    print 'True in list\n\t',
    printtimes(timeit.Timer('True in [i in a for i in b]',setup=setup1).timeit(10000000))
    printtimes(timeit.Timer('True in [i in a for i in b]',setup=setup2).timeit(10000000))
    printtimes(timeit.Timer('True in [i in a for i in b]',setup=setup3).timeit(10000000))
    print
    
    print 'Set with &\n\t',
    printtimes(timeit.Timer('bool(set(a) & set(b))',setup=setup1).timeit(10000000))
    printtimes(timeit.Timer('bool(set(a) & set(b))',setup=setup2).timeit(10000000))
    printtimes(timeit.Timer('bool(set(a) & set(b))',setup=setup3).timeit(10000000))
    print
    
    print 'Set intersection explcit set(b)\n\t',
    printtimes(timeit.Timer('bool(set(a).intersection(set(b)))',setup=setup1).timeit(10000000))
    printtimes(timeit.Timer('bool(set(a).intersection(set(b)))',setup=setup2).timeit(10000000))
    printtimes(timeit.Timer('bool(set(a).intersection(set(b)))',setup=setup3).timeit(10000000))
    print
    
    print 'Set intersection implicit set(b)\n\t',
    printtimes(timeit.Timer('bool(set(a).intersection(b))',setup=setup1).timeit(10000000))
    printtimes(timeit.Timer('bool(set(a).intersection(b))',setup=setup2).timeit(10000000))
    printtimes(timeit.Timer('bool(set(a).intersection(b))',setup=setup3).timeit(10000000))
    print
    
    print 'Set isdisjoint explicit set(b)\n\t',
    printtimes(timeit.Timer('not set(a).isdisjoint(set(b))',setup=setup1).timeit(10000000))
    printtimes(timeit.Timer('not set(a).isdisjoint(set(b))',setup=setup2).timeit(10000000))
    printtimes(timeit.Timer('not set(a).isdisjoint(set(b))',setup=setup3).timeit(10000000))
    print
    
    print 'Set isdisjoint implicit set(b)\n\t',
    printtimes(timeit.Timer('not set(a).isdisjoint(b)',setup=setup1).timeit(10000000))
    printtimes(timeit.Timer('not set(a).isdisjoint(b)',setup=setup1).timeit(10000000))
    printtimes(timeit.Timer('not set(a).isdisjoint(b)',setup=setup3).timeit(10000000))
    print
    
    0 讨论(0)
  • 2020-11-22 17:51

    Best I could come up with:

    any([True for e in (1, 2) if e in a])
    
    0 讨论(0)
  • 2020-11-22 17:54

    Think about what the code actually says!

    >>> (1 or 2)
    1
    >>> (2 or 1)
    2
    

    That should probably explain it. :) Python apparently implements "lazy or", which should come as no surprise. It performs it something like this:

    def or(x, y):
        if x: return x
        if y: return y
        return False
    

    In the first example, x == 1 and y == 2. In the second example, it's vice versa. That's why it returns different values depending on the order of them.

    0 讨论(0)
  • 2020-11-22 17:54

    1 line without list comprehensions.

    >>> any(map(lambda each: each in [2,3,4], [1,2]))
    True
    >>> any(map(lambda each: each in [2,3,4], [1,5]))
    False
    >>> any(map(lambda each: each in [2,3,4], [2,4]))
    True
    
    0 讨论(0)
提交回复
热议问题