擅长:python、mysql、java
<p>您表示输出的方式令人困惑,但由于您要从一组5张卡中选择3张卡,因此需要5C3。您可以使用<code>itertools.combinations</code>实现这一点。你知道吗</p>
<p>从<a href="https://docs.python.org/2.7/library/itertools.html#itertools.combinations" rel="nofollow">doc</a>:</p>
<p><strong>itertools.组合(iterable,r)</strong>:</p>
<blockquote>
<p>Return r length subsequences of elements from the input iterable.</p>
<p>Combinations are emitted in lexicographic sort order. So, if the input
iterable is sorted, the combination tuples will be produced in sorted
order.</p>
<p>Elements are treated as unique based on their position, not on their
value. So if the input elements are unique, there will be no repeat
values in each combination.</p>
</blockquote>
<p>导入itertools</p>
<pre><code>a = [0,1,2,3,4]
print [p for p in itertools.combinations(a, 3)]
</code></pre>
<p><strong>输出:</strong></p>
<blockquote>
<p><code>[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4),(1,2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]</code></p>
</blockquote>