擅长:python、mysql、java
<p>可以在<a href="https://docs.python.org/2/reference/compound_stmts.html#the-for-statement" rel="noreferrer">^{<cd1>} statement</a>中使用元组解包:</p>
<pre><code>for i, (key, value) in enumerate(a.iteritems()):
# Do something with i, key, value
</code></pre>
<hr/>
<pre><code>>>> d = {'a': 'b'}
>>> for i, (key, value) in enumerate(d.iteritems()):
... print i, key, value
...
0 a b
</code></pre>
<p>旁注:</p>
<p>在Python 3.x中,使用<a href="https://docs.python.org/3/library/stdtypes.html#dict.items" rel="noreferrer">^{<cd2>}</a>返回一个iterable字典视图。</p>
<pre><code>>>> for i, (key, value) in enumerate(d.items()):
... print(i, key, value)
</code></pre>