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

前端 未结 14 2076
终归单人心
终归单人心 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:37
    a = {2,3,4}
    if {1,2} & a:
        pass
    

    Code golf version. Consider using a set if it makes sense to do so. I find this more readable than a list comprehension.

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

    Maybe a bit more lazy:

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

    In python 3 we can start make use of the unpack asterisk. Given two lists:

    bool(len({*a} & {*b}))
    

    Edit: incorporate alkanen's suggestion

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

    This will do it in one line.

    >>> a=[2,3,4]
    >>> b=[1,2]
    >>> bool(sum(map(lambda x: x in b, a)))
    True
    
    0 讨论(0)
  • 2020-11-22 17:47

    When you think "check to see if a in b", think hashes (in this case, sets). The fastest way is to hash the list you want to check, and then check each item in there.

    This is why Joe Koberg's answer is fast: checking set intersection is very fast.

    When you don't have a lot of data though, making sets can be a waste of time. So, you can make a set of the list and just check each item:

    tocheck = [1,2] # items to check
    a = [2,3,4] # the list
    
    a = set(a) # convert to set (O(len(a)))
    print [i for i in tocheck if i in a] # check items (O(len(tocheck)))
    

    When the number of items you want to check is small, the difference can be negligible. But check lots of numbers against a large list...

    tests:

    from timeit import timeit
    
    methods = ['''tocheck = [1,2] # items to check
    a = [2,3,4] # the list
    a = set(a) # convert to set (O(n))
    [i for i in tocheck if i in a] # check items (O(m))''',
    
    '''L1 = [2,3,4]
    L2 = [1,2]
    [i for i in L1 if i in L2]''',
    
    '''S1 = set([2,3,4])
    S2 = set([1,2])
    S1.intersection(S2)''',
    
    '''a = [1,2]
    b = [2,3,4]
    any(x in a for x in b)''']
    
    for method in methods:
        print timeit(method, number=10000)
    
    print
    
    methods = ['''tocheck = range(200,300) # items to check
    a = range(2, 10000) # the list
    a = set(a) # convert to set (O(n))
    [i for i in tocheck if i in a] # check items (O(m))''',
    
    '''L1 = range(2, 10000)
    L2 = range(200,300)
    [i for i in L1 if i in L2]''',
    
    '''S1 = set(range(2, 10000))
    S2 = set(range(200,300))
    S1.intersection(S2)''',
    
    '''a = range(200,300)
    b = range(2, 10000)
    any(x in a for x in b)''']
    
    for method in methods:
        print timeit(method, number=1000)
    

    speeds:

    M1: 0.0170331001282 # make one set
    M2: 0.0164539813995 # list comprehension
    M3: 0.0286040306091 # set intersection
    M4: 0.0305438041687 # any
    
    M1: 0.49850320816 # make one set
    M2: 25.2735087872 # list comprehension
    M3: 0.466138124466 # set intersection
    M4: 0.668627977371 # any
    

    The method that is consistently fast is to make one set (of the list), but the intersection works on large data sets the best!

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

    Simple.

    _new_list = []
    for item in a:
        if item in b:
            _new_list.append(item)
        else:
            pass
    
    0 讨论(0)
提交回复
热议问题