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

前端 未结 14 2063
终归单人心
终归单人心 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:57

    >>> L1 = [2,3,4]
    >>> L2 = [1,2]
    >>> [i for i in L1 if i in L2]
    [2]
    
    
    >>> S1 = set(L1)
    >>> S2 = set(L2)
    >>> S1.intersection(S2)
    set([2])
    

    Both empty lists and empty sets are False, so you can use the value directly as a truth value.

提交回复
热议问题