请不要给我负号。我想问一个更好的方法来解决这个问题,因为我现在所做的正在成为我的巨大负担。
这是我的问题:我有两个列表。我想确保一个列表中的任何项目都不在另一个列表中。
在 Python 中,我一直在处理以下几行...(假设 List_s 有 3 个项目。)
if List_s[0] not in List_big and List_s[1] not in List_big and List_s[2] not in List_big: #none item from List_s should be in List_big
do something
else:
pass
这些行对我来说实际上还可以,直到我突然意识到我必须处理长度大于 200 的列表。我有很多列表可以比较。
那么我该如何更改代码呢?非常感谢您的帮助!
请您参考如下方法:
您可以将其中一个列表转换为集合 并使用set.intersection :
if not set(List_s).intersection(List_big):
print('no common items between lists')
注意两个列表中的元素必须是hashable .
