擅长:python、mysql、java
<p>好的。我用了-1itertools.排列:(</p>
<p>似乎你需要不使用itertools的重复排列。
给你:</p>
<pre><code>def permutation_with_repitition(items, prefix):
if len(prefix) == len(items):
yield prefix
else:
for item in items:
prefix.append(item)
for p in permutation_with_repitition(items, prefix):
yield p
prefix.pop()
L = [1,2,3]
for p in permutation_with_repitition(L, []):
print p
</code></pre>
<p>输出:</p>
<pre><code>[1, 1, 1]
[1, 1, 2]
[1, 1, 3]
[1, 2, 1]
[1, 2, 2]
[1, 2, 3]
[1, 3, 1]
[1, 3, 2]
[1, 3, 3]
[2, 1, 1]
[2, 1, 2]
[2, 1, 3]
[2, 2, 1]
[2, 2, 2]
[2, 2, 3]
[2, 3, 1]
[2, 3, 2]
[2, 3, 3]
[3, 1, 1]
[3, 1, 2]
[3, 1, 3]
[3, 2, 1]
[3, 2, 2]
[3, 2, 3]
[3, 3, 1]
[3, 3, 2]
[3, 3, 3]
</code></pre>