# loops - 如果 list(a) 中的项存在于列表( b ) 中，python 通过更大的list(b) 迭代 list(a) 以给出 true/false 值

88 5

``````
a = ['A', 'B', 'C', 'D']

b = ['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']

``````

``````
c = [False, True, True, True]

``````

``````
c = [False for i in range(len(a))]

for i in a:

for j in b:

if a[i] == b[j]:

c[i] = True

``````

TypeError: 列表索引必须是整数，而不是 str

c = [True for i in a if i in b ]

c = [True, True ]

c = [False, True, True ]

137 5
``````
a = ['A', 'B', 'C', 'D']

b = ['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']

c = [x in b for x in a]

``````
100 2

``````
a = ['A', 'B', 'C', 'D']

b = ['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']

c = [True if element in b else False for element in a]

print(c)

# [False, True, True, True]

``````
96 5
``````
# gives you a set of elements that are common for both lists.

intersect = set(a) & set(b)

c = [x in intersect for x in a]

``````
114 2

``````
l = ['A', 'B', 'C']

print('A' in l) # True

print('Z' in l) # False

``````

``````
a = ['A', 'B', 'C', 'D']

b = ['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']

c = [x in b for x in a]

print(c) # [False, True, True, True]

``````

``````
a = ['A', 'B', 'C', 'D']

b = ['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']

b_set = set(b)

c = [x in b_set for x in a]

``````

``````
a = ['A', 'B', 'C', 'D']

b = {'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'}

c = [x in b for x in a]

``````