擅长:python、mysql、java
<pre><code>>>> mylist = [(1,'one'),(2,'two'),(3,'three')]
>>> [j for i,j in mylist]
['one', 'two', 'three']
>>> [i for i,j in mylist]
[1, 2, 3]
</code></pre>
<p>这是使用<a href="http://docs.python.org/tutorial/datastructures.html" rel="noreferrer">list comprehension (have a look at this link)</a>。因此它遍历<code>mylist</code>中的元素,依次将<code>i</code>和<code>j</code>设置为元组中的两个元素。它实际上相当于:</p>
<pre><code>>>> newlist = []
>>> for i, j in mylist:
... newlist.append(i)
...
>>> newlist
[1, 2, 3]
</code></pre>