use a list comprehension:
In [164]: A = [1,2,3,4,5,6,7,8]
In [165]: B = [2,3,4]
In [166]: [x in B for x in A]
Out[166]: [False, True, True, True, False, False, False, False]
If B
is huge then better convert it to a set
first. As, membership test for sets is O(1)
compared to O(n)
in lists.
In [167]: b=set(B)
In [168]: [x in b for x in A]
Out[168]: [False, True, True, True, False, False, False, False]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…