我正在開發(fā)一個程序,該程序?qū)⒔o出重復(fù)的值列表的所有組合。只要[10,9,8]和[8,9,10]都不可能,結(jié)果的排序方式就無關(guān)緊要。到目前為止,我有一段代碼可以滿足我的要求,但是我希望結(jié)果以列表格式而不是元組的形式出現(xiàn)。它使用了sage庫中的unordered_tuples函數(shù),但所做的只是返回一個列表列表,如下所示:>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 productdef 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 輸出:[((([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]]]
2 回答

阿波羅的戰(zhàn)車
TA貢獻(xiàn)1862條經(jīng)驗 獲得超6個贊
output = [[[tuple[0]], [0], [tuple[1]], [0]] for tuple in unordered_tuples([10,9,8],2)]

蕭十郎
TA貢獻(xiàn)1815條經(jīng)驗 獲得超13個贊
從PostgreSQL數(shù)據(jù)庫提取數(shù)據(jù)時遇到了類似的問題。這是我使用的方法。
def tupleToList(t):
l = []
for i in xrange(len(t)):
l.append(list(t[i]))
pass
return l
pass
這適用于2D列表。在您的情況下,我正在努力將其添加到5D列表中。
添加回答
舉報
0/150
提交
取消