在Python中有效地知道两个列表的交集是否为空 [英] efficiently knowing if intersection of two list is empty or not, in python
问题描述
假设我有两个列表,L和M.现在我想知道它们是否共享一个元素. 哪一种是最快的询问(在python中)是否共享元素的方法? 我不在乎它们共享哪些元素,或者共享或不共享多少元素.
Suppose I have two lists, L and M. Now I want to know if they share an element. Which would be the fastest way of asking (in python) if they share an element? I don't care which elements they share, or how many, just if they share or not.
例如,在这种情况下
L = [1,2,3,4,5,6]
M = [8,9,10]
我应该得到False,在这里:
I should get False, and here:
L = [1,2,3,4,5,6]
M = [5,6,7]
我应该得到True.
我希望问题清楚. 谢谢!
I hope the question's clear. Thanks!
Manuel
推荐答案
或更简洁地
if set(L) & set(M):
# there is an intersection
else:
# no intersection
如果您确实需要True
或False
bool(set(L) & set(M))
经过一些计时后,这似乎也是尝试的好选择
After running some timings, this seems to be a good option to try too
m_set=set(M)
any(x in m_set for x in L)
如果M或L中的项目不可散列,则必须使用这种效率较低的方法
If the items in M or L are not hashable you have to use a less efficient approach like this
any(x in M for x in L)
以下是100个商品列表的一些时间安排.在没有交集的情况下,使用集合的速度要快得多,在有交集的情况下,使用集的速度会慢一些.
Here are some timings for 100 item lists. Using sets is considerably faster when there is no intersection, and a bit slower when there is a considerable intersection.
M=range(100)
L=range(100,200)
timeit set(L) & set(M)
10000 loops, best of 3: 32.3 µs per loop
timeit any(x in M for x in L)
1000 loops, best of 3: 374 µs per loop
timeit m_set=frozenset(M);any(x in m_set for x in L)
10000 loops, best of 3: 31 µs per loop
L=range(50,150)
timeit set(L) & set(M)
10000 loops, best of 3: 18 µs per loop
timeit any(x in M for x in L)
100000 loops, best of 3: 4.88 µs per loop
timeit m_set=frozenset(M);any(x in m_set for x in L)
100000 loops, best of 3: 9.39 µs per loop
# Now for some random lists
import random
L=[random.randrange(200000) for x in xrange(1000)]
M=[random.randrange(200000) for x in xrange(1000)]
timeit set(L) & set(M)
1000 loops, best of 3: 420 µs per loop
timeit any(x in M for x in L)
10 loops, best of 3: 21.2 ms per loop
timeit m_set=set(M);any(x in m_set for x in L)
1000 loops, best of 3: 168 µs per loop
timeit m_set=frozenset(M);any(x in m_set for x in L)
1000 loops, best of 3: 371 µs per loop
这篇关于在Python中有效地知道两个列表的交集是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!