我想检查一个列表中的项目的 任何是否存在于另一个列表中。我可以简单地用下面的代码来完成,但是我怀疑可能有一个库函数来完成这项工作。如果没有,是否有更简单的方法达到同样的结果。
In [78]: a = [1, 2, 3, 4, 5]
In [79]: b = [8, 7, 6]
In [80]: c = [8, 7, 6, 5]
In [81]: def lists_overlap(a, b):
....: for i in a:
....: if i in b:
....: return True
....: return False
....:
In [82]: lists_overlap(a, b)
Out[82]: False
In [83]: lists_overlap(a, c)
Out[83]: True
In [84]: def lists_overlap2(a, b):
....: return len(set(a).intersection(set(b))) > 0
....: