擅长:python、mysql、java
<p>如果不想使用<code>itertools</code>,请使用<a href="http://docs.python.org/2/library/itertools.html#itertools.combinations">documented pure-Python equivalent</a>:</p>
<pre><code>def combinations(iterable, r):
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = range(r)
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)
</code></pre>