第一种,for循环,此情况是list1属于list2
list1 = [1, 2, 3]
list2 = [1, 2, 3, 4, 5]
for i in list2:
if i not in list1:
print(i)
第二种,得出列表中相同的元素,和不同元素(两个列表都存在)
list1 = [1, 2, 3, 8]
list2 = [1, 2, 3, 4, 5]
a = [x for x in list1 if x in list2] #列表中相同元素
b = [y for y in (list1 + list2) if y not in a] #列表中不同元素 (两个列表都存在)
print(a)
print(b)
第三种 c 为 在list1列表中而不在list2列表中
d 为 在list2列表中而不在list1列表中
list1 = [1, 2, 3, 8]
list2 = [1, 2, 3, 4, 5]
c = [x for x in list1 if x not in list2]
d = [y for y in list2 if y not in list1]
print(c)
print(d)