我正在开发一个程序,该程序将给出重复的值列表的所有组合。只要[10,9,8]和[8,9,10]都不可能,结果的排序方式就无关紧要。到目前为止,我有一段代码可以满足我的要求,但是我希望结果以列表格式而不是元组的形式出现。它使用了sage库中的unordered_tuples函数,但所做的只是返回一个列表列表,如下所示:
>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]]
和:
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)
输出:
[((([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])]
想:
[[[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]]]
阿波罗的战车
萧十郎
相关分类