我有这样的事情:
a = ['a','b','c']
b = ['a','t','g','c','b']
和
def check_list(a, b):
for entry in a:
if entry not in b:
return False
return True
如何做得好?
答案 0 :(得分:3)
您可以使用集合运算符:
>>> a = ['a','b','c']
>>> b = ['a','t','g','c','b']
>>> set(a) <= set(b)
True
如果您需要处理重复项:
>>> from collections import Counter
>>> cb = Counter(b)
>>> cb.subtract(Counter(a))
>>> all(count >= 0 for count in cb.values())
True
答案 1 :(得分:2)
您可以使用set.issubset
:
>>> a = ['a','b','c']
>>> b = ['a','t','g','c','b']
>>> set(a).issubset(b)
True
答案 2 :(得分:0)
试试这个:
a = ['a', 'b', 'c']
b = ['a', 't', 'g', 'c', 'b']
print(all(item in b for item in a))
输出:
True