`list1 = [[1, 2, 3], [4, 5, 6]]
list2 = [1 ,8 ,7 ,2, 0, 3]`
输出应该显示,list2包含list1子列表中的所有整数。
考虑副本的版本:
from collections import Counter
list_1 = [[1, 2, 3], [4, 5, 6]]
list_2 = [1, 8, 7, 2, 0, 3]
counts = Counter(list_2)
res = any(len(Counter(e) - counts) == 0 for e in list_1)
print(res)
True
如果列表中没有重复项:
s2 = set(list2)
result = any(all(e in s2 for e in sub) for sub in list1)
如果可能存在重复,并且您需要所有的出现都出现在list2
中,您可以使用collections.Counter
:
from collections import Counter
c2 = Counter(list2)
result = any(not (Counter(sub) - c2) for sub in list1)