擅长:python、mysql、java
<p>函数<a href="https://docs.python.org/2.7/library/itertools.html#itertools.product" rel="nofollow">^{<cd1>}</a>将完成以下操作:</p>
<pre><code>In [41]: a, b = [1, 2], [3, 4]
In [42]: from itertools import product
In [43]: list(product(a, b))
Out[43]: [(1, 3), (1, 4), (2, 3), (2, 4)]
</code></pre>
<p>需要指出的是,您需要调用内置函数<code>list()</code>,将<code>product</code>返回的迭代器转换为列表。你知道吗</p>
<p>顺便说一句,如果您的目标是计算列表本身的乘积,您可能会发现使用可选的<code>repeat</code>关键字参数很方便:</p>
<pre><code>In [46]: list(product(a, repeat=3))
Out[46]:
[(1, 1, 1),
(1, 1, 2),
(1, 2, 1),
(1, 2, 2),
(2, 1, 1),
(2, 1, 2),
(2, 2, 1),
(2, 2, 2)]
</code></pre>