list1 = [1, 2, 3, 4]
list2 = [1, 2, 3]
no_exist = [False for a in list2 if a not in list1]
if no_exist:
print('list2 not exist list1')
else:
print('list2 exist list1')
本文介绍了一种使用Python检查两个列表间元素包含关系的方法。通过遍历第二个列表并判断其元素是否存在于第一个列表中,实现了对列表元素的高效对比。此方法适用于需要验证数据一致性或进行初步数据清洗的场景。
list1 = [1, 2, 3, 4]
list2 = [1, 2, 3]
no_exist = [False for a in list2 if a not in list1]
if no_exist:
print('list2 not exist list1')
else:
print('list2 exist list1')
1337

被折叠的 条评论
为什么被折叠?