Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Or more concisely</p> <pre><code>if set(L) &amp; set(M): # there is an intersection else: # no intersection </code></pre> <p>If you really need <code>True</code> or <code>False</code></p> <pre><code>bool(set(L) &amp; set(M)) </code></pre> <p>After running some timings, this seems to be a good option to try too</p> <pre><code>m_set=set(M) any(x in m_set for x in L) </code></pre> <p>If the items in M or L are not hashable you have to use a less efficient approach like this</p> <pre><code>any(x in M for x in L) </code></pre> <p>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.</p> <pre><code>M=range(100) L=range(100,200) timeit set(L) &amp; 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) &amp; 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) &amp; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload