<p>我个人喜欢这种方法:</p>
<pre><code>>>> li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
>>>
>>> v = iter(li)
>>> li = [(i, next(v), next(v)) for i in v] # creates list of tuples
>>>
>>> v = iter(li)
>>> li = [[i, next(v)] for i in v] # creates list of lists of tuples
>>>
>>> li
[[(1, 2, 3), (4, 5, 6)], [(7, 8, 9), (10, 11, 12)], [(13, 14, 15), (16, 17, 18)]]
</code></pre>
<p>顺便说一句,如果元素不适合这样的结构,这将引发错误,例如,请考虑:</p>
<pre><code>>>> l = [1, 2, 3]
>>> v = iter(l)
>>>
>>> [(i, next(v)) for i in v]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
</code></pre>
<p>这可能合适,也可能不合适。你知道吗</p>
<p>如果不是,那么简单的解决方法是给<code>next()</code>第二个参数:</p>
<pre><code>>>> l = [1, 2, 3]
>>> v = iter(l)
>>>
>>> [(i, next(v, None)) for i in v]
[(1, 2), (3, None)]
</code></pre>
<hr/>
<p><strong>参考号:</strong></p>
<ul>
<li><a href="http://docs.python.org/2/library/functions.html#iter" rel="nofollow">^{<cd2>}</a></li>
<li><a href="http://docs.python.org/2/library/functions.html#next" rel="nofollow">^{<cd1>}</a></li>
<li><a href="http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions" rel="nofollow">List Comprehensions</a></li>
</ul>