【发布时间】:2019-11-17 12:22:45
【问题描述】:
我想在 Python (2.7) 中将两个列表相交。我需要结果是可迭代的:
list1 = [1,2,3,4]
list2 = [3,4,5,6]
result = (3,4) # any kind of iterable
提供一个完整的迭代会在交集之后首先执行,以下哪个更有效?
使用生成器:
result = (x for x in list1 if x in list2)
使用过滤器():
result = filter(lambda x: x in list2, list1)
其他建议?
提前致谢,
暗嫩
【问题讨论】:
标签: python list intersection python-2.7 intersect