Python to determine whether a list contains all the elements of another list

There are two main ways: use for to judge element by element, use set to judge whether it is a subset
Method 1 :

a = [1, 2, 3, 4, 5, 6]
b = [1, 3, 5]
  
d = [False for c in b if c not in a]
if d:
    print('a 不包含 b 的所有元素!')
else:
    print('a 包含 b 的所有元素!')

Method two :

a = [1, 2, 3, 4, 5, 6]
b = [2, 4, 6]
print(set(b) < set(a))  # 判断 b 是否是 a 的真子集,<= 表示是否是子集
# 输出:
True

Guess you like

Origin blog.csdn.net/iuv_li/article/details/127533463