Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I <em>think</em> this is what you want, although it's not that clear:</p> <pre><code>for i in range(0, len(ListB), 2): for x in ListA: if ListB[i] &lt; x &lt; ListB[i+1]: print 'within range' </code></pre> <p>You're right in your intuition that you can <code>i:i+2</code> sort of represents a "range", but not quite like that. That syntax can only be used when slicing a <code>list</code>: <code>ListB[i:i+2]</code> returns a smaller list with just 2 elements, <code>[ListB[i], ListB[i+1]]</code>, just as you expect, but <code>i:i+2</code> on its own is illegal. (If you really want to, you can write <code>slice(i, i+2)</code> to mean what you're aiming at, but it's not all that useful here.)</p> <p>Also, from your description, you want to compare <code>x</code> to the values <code>ListB[i]</code> and <code>ListB[i+1]</code>, not just <code>i</code> and <code>i+1</code> as in your code.</p> <p>If you wanted to use the slice to generate a range, you could do so, but only indirectly, and it would actually be clumsier and worse in most ways:</p> <pre><code>for i in range(0, len(ListB), 2): brange = range(*ListB[i:i+2]) for x in ListA: if x in brange: print 'within range' </code></pre> <p>But this is checking for a half-open range rather than an open range, and it requires creating the list representing the range, and walking over the whole thing, and it will only work for integers. So, it's probably not what you want. Better to just explicitly use <code>ListB[i] &lt; x &lt; ListB[i+1]</code>.</p> <p>You might want to consider whether there's a more readable way to build a list of pairs of elements from <code>ListB</code>. Without yet knowing about <code>itertools</code> and list comprehensions or generator expressions (I assume), you'd probably have to write something pretty verbose and explicit, but it might be useful practice anyway.</p>
    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.
 

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