Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="http://docs.python.org/library/itertools.html?highlight=itertools#itertools.islice" rel="nofollow"><code>itertools.islice</code></a> function can produce a slice from any iterable. The statement:</p> <pre><code>itertools.islice(lst, 0, None, 2) </code></pre> <p>In simplified terms means "return an iterator which, when evaluated, will return every other element in lst starting with the element at 0". The <code>0</code> arg is the starting point and <code>2</code> is the step value (how many to skip to get the next value)</p> <p>It would be better to use a 6-element list to illustrate, since looking at <code>[(1, 2), (1, 5)]</code> it may be unclear which function produces the inner tuples (they are not produced until the final izip).</p> <pre><code>&gt;&gt;&gt; lst = [1, 2, 3, 4, 5, 6] &gt;&gt;&gt; list(itertools.islice(lst, 0, None, 2)) [1, 3, 5] &gt;&gt;&gt; list(itertools.islice(lst, 0, None, 2)) [2, 4, 6] </code></pre> <p>Be careful using this with a generator; the first call will traverse the whole sequence, draining the generator:</p> <pre><code>&gt;&gt;&gt; def foo(): ... for i in range(6): ... yield i + 1 &gt;&gt;&gt; &gt;&gt;&gt; gen = foo() &gt;&gt;&gt; list(itertools.islice(gen, 0, None, 2) [1, 3, 5] &gt;&gt;&gt; list(itertools.islice(gen, 1, None, 2) [] </code></pre> <p>Your function needs to produce 2 sequences, odds and evens. This is where the list comprehension comes in:</p> <pre><code>&gt;&gt;&gt; [itertools.islice(lst, i, None, 2) for i in range(2)] [&lt;itertools.islice object at 0x7f958a79eaf8&gt;, &lt;itertools.islice object at 0x7f958a79eaa0&gt;] </code></pre> <p>Now you have 2 islice objects ready to be interleaved. To do this we could use <a href="http://docs.python.org/library/functions.html?highlight=zip#zip" rel="nofollow"><code>zip</code></a>, but <a href="http://docs.python.org/library/itertools.html?highlight=itertools#itertools.izip" rel="nofollow"><code>itertools.izip</code></a> is more efficient because it returns an iterator:</p> <pre><code>&gt;&gt;&gt; list(zip([1, 3, 5], [2, 4, 6])) [(1, 2), (3, 4), (5, 6)] &gt;&gt;&gt; tmp = itertools.izip(*[itertools.islice(lst, i, None, 2) for i in range(2)]) &gt;&gt;&gt; tmp &lt;itertools.izip object at 0x7f958a7b6cf8&gt; &gt;&gt;&gt; list(tmp) [(1, 2), (3, 4), (5, 6)] </code></pre> <p>Hope that helps clarify the steps.</p>
    singulars
    1. This table or related slice is empty.
    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