Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You mix up <a href="http://docs.python.org/library/functions.html#map" rel="nofollow"><code>map()</code></a> and <a href="http://docs.python.org/library/functions.html#zip" rel="nofollow"><code>zip()</code></a> or <a href="http://docs.python.org/library/functions.html#func-dict" rel="nofollow"><code>dict()</code></a>.</p> <p><code>map</code> operates on iterables and takes two arguments, a function and an iterable:</p> <pre><code>&gt;&gt;&gt; def addOne(x): ... return x+1 ... &gt;&gt;&gt; l = [1, 2, 3, 4] &gt;&gt;&gt; print(map(addOne, l)) [2, 3, 4, 5] </code></pre> <p>So it applies the function passed as first argument to each element of the sequence and returns the new sequence as list (note that this behaviour is different in python3, where you get an iterable instead of a list).</p> <p><code>zip()</code> instead does what you want. It takes an arbitary amount of iterables and merges together each iteration step to one tuple:</p> <pre><code>&gt;&gt;&gt; l1, l2 = [1, 2, 3, 4], [5, 6, 7, 8] &gt;&gt;&gt; print(zip(l1, l2)) [(1, 5), (2, 6), (3, 7), (4, 8)] </code></pre> <p>Again, the result value is a list in python2 and an iterator in python3. You can iterate over that using a normal for loop like you did in your question. Of such a zipped iterator you can also create a <code>dict</code> (which might also be called a <em>map</em>, as it maps items to values):</p> <pre><code>&gt;&gt;&gt; d = dict(zip(l1, l2)) &gt;&gt;&gt; d[1] 5 &gt;&gt;&gt; d {1: 5, 2: 6, 3: 7, 4: 8} </code></pre> <p>However, you can not directly iterate over key-value pairs in dicts:</p> <pre><code>&gt;&gt;&gt; for key, value in d: ... print(key) ... Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; TypeError: 'int' object is not iterable </code></pre> <p>This is a rather obscure error message, as we passed a <code>dict</code> not an <code>int</code>! Where the heck does the <code>int</code> come from? By default, when iterating over a <code>dict</code>, one will iterate over the keys:</p> <pre><code>&gt;&gt;&gt; list(d) [1, 2, 3, 4] </code></pre> <p>But we instructed python to do <a href="http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences" rel="nofollow"><em>unpacking</em></a> on our values. So it tries to get two values out of one int, which obviously does not work. In that light, the error seems reasonable. So to iterate over key-value pairs one has to use:</p> <pre><code>&gt;&gt;&gt; for key, value in d.items(): ... print(key, value) ... 1 5 2 6 3 7 4 8 </code></pre> <p>(output might differ depending on whether you imported the <code>print_function</code> from <code>__future__</code>). For large dicts and on python2, <code>iteritems()</code> will be faster than <code>items()</code>, as <code>items()</code> puts all key-value pairs in a list first, while <code>iteritems()</code> creates only a lazy iterator. On python3, <code>items()</code> does something similar to that already.</p> <p>I hope that clarifies the situation a bit.</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.
    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