回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在做一个程序,它能给出所有值的组合,而不会重复。结果如何排序并不重要,只要[10,9,8]和[8,9,10]都不可能。到目前为止,我有一段代码,做我想要的,但我希望结果是在列表格式,而不是元组。它使用sage库中的unordered\u tuples函数,但只返回一个列表列表,如下所示:</p>
<pre><code>>from sage.all import unordered_tuples
>tuples = unordered_tuples([10,9,8],2)
>print tuples
[[8, 8], [8, 9], [8, 10], [9, 9], [9, 10], [10, 10]]
</code></pre>
<p>以及:</p>
<pre><code>from itertools import product
def combos():
dicti = {}
index = 0
for entry in [1,0,1,0]:
dicIndex = str('0')+str(index)
print dicIndex
if entry == 0: dicti[dicIndex] = [[0]]
else: dicti[str('0')+str(index)] = unordered_tuples([10,9,8],entry)
index += 1
lis = ['00','01','02','03']
value = dicti[lis[0]]
print dicti
for index in lis[1:]:
value = product(value, dicti[index])
if index == lis[-1]:
print list(value)
print
</code></pre>
<p>输出:</p>
<pre><code>[((([8], [0]), [8]), [0]), ((([8], [0]), [9]), [0]), ((([8], [0]), [10]), [0]), ((([9], [0]), [8]), [0]), ((([9], [0]), [9]), [0]), ((([9], [0]), [10]), [0]), ((([10], [0]), [8]), [0]), ((([10], [0]), [9]), [0]), ((([10], [0]), [10]), [0])]
</code></pre>
<p>想要:</p>
<pre><code>[[[8], [0], [8], [0]], [[8], [0], [9], [0]], [[8], [0], [10], [0]], [[9], [0], [8], [0]], [[9], [0], [9], [0]], [[9], [0], [10], [0]], [[10], [0], [8], [0]], [[10], [0], [9], [0]], [[10], [0], [10], [0]]]
</code></pre>