我要检查,如果一组单词出现在另一组单词,例如计数器在python集合包中使用的算法?
list1=['Hello','World']
list2=['Hello','World','Good','Bye']
我写了下面的代码,以检查是否出现在列表1的话也出现在列表2
def check(list1,list2):
for l in list1:
if l not in list2:
return False
return True
但这代码失败的大input.Then我发现下面的代码在其净值适用于所有的输入
from collections import Counter
def check(list1,list2):
return not (Counter(list1) - Counter(list2))
谁能告诉我什么algorit hm不会使用或提供任何其他方法,使用相同的结果可以在不使用内置函数的情况下实现。
您可以检查出的[代码](HTTPS://hg.python。 org/cpython/file/3.6/Lib/collections/__ init__.py)。虽然毫无疑问的一些实现是在C中。它提到'multiset'并给出了一些参考链接。 –