擅长:python、mysql、java
<p>使用<a href="http://docs.python.org/2/library/functions.html#zip" rel="nofollow">^{<cd1>} built-in function</a>将两个列表的值配对:</p>
<pre><code>for row1value, row2value in zip(row1, row2):
# do something with row1value and row2value
</code></pre>
<p>如果要将row1中的每个元素与row2的每个元素(这两个列表的乘积)组合起来,请改用<a href="http://docs.python.org/2/library/itertools.html#itertools.product" rel="nofollow">^{<cd2>}</a>:</p>
^{pr2}$
<p><code>zip()</code>只需将生成<code>len(shortest_list)</code>项的列表配对,<code>product()</code>将一个列表中的每个元素与另一个列表中的每个元素配对,生成<code>len(list1)</code>次<code>len(list2)</code>项:</p>
<pre><code>>>> row1 = [1, 2, 3]
>>> row2 = [9, 8, 7]
>>> for a, b in zip(row1, row2):
... print a, b
...
1 9
2 8
3 7
>>> from itertools import product
>>> for a, b in product(row1, row2):
... print a, b
...
1 9
1 8
1 7
2 9
2 8
2 7
3 9
3 8
3 7
</code></pre>