Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The other great answers and comments explain well the roles of <a href="http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists" rel="noreferrer"><strong>argument unpacking</strong></a> and <a href="http://docs.python.org/library/functions.html#zip" rel="noreferrer"><strong>zip()</strong></a>.</p> <p>As <a href="https://stackoverflow.com/questions/2233204/how-does-zipitersn-work-in-python/2233247#2233247">Ignacio</a> and <a href="https://stackoverflow.com/questions/2233204/how-does-zipitersn-work-in-python/2233289#2233289">ujukatzel</a> say, you pass to <code>zip()</code> three references to the same iterator and <code>zip()</code> makes 3-tuples of the integers—in order—from each reference to the iterator:</p> <pre><code>1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9 ^ ^ ^ ^ ^ ^ ^ ^ ^ </code></pre> <p>And since you ask for a more verbose code sample:</p> <pre><code>chunk_size = 3 L = [1,2,3,4,5,6,7,8,9] # iterate over L in steps of 3 for start in range(0,len(L),chunk_size): # xrange() in 2.x; range() in 3.x end = start + chunk_size print L[start:end] # three-item chunks </code></pre> <p>Following the values of <code>start</code> and <code>end</code>: </p> <pre><code>[0:3) #[1,2,3] [3:6) #[4,5,6] [6:9) #[7,8,9] </code></pre> <p>FWIW, you can get the same result with <code>map()</code> with an initial argument of <code>None</code>:</p> <pre><code>&gt;&gt;&gt; map(None,*[iter(s)]*3) [(1, 2, 3), (4, 5, 6), (7, 8, 9)] </code></pre> <p>For more on <code>zip()</code> and <code>map()</code>: <a href="http://muffinresearch.co.uk/archives/2007/10/16/python-transposing-lists-with-map-and-zip/" rel="noreferrer">http://muffinresearch.co.uk/archives/2007/10/16/python-transposing-lists-with-map-and-zip/</a></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. 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