[英]list of lists rather than list of tuples - python
I'm working on a program that will give all combinations of list of values without repetition. 我正在开发一个程序,该程序将给出重复的值列表的所有组合。 It doesn't matter how the result is sorted so long as both [10, 9, 8] and [8, 9, 10] are not both possible.
只要[10,9,8]和[8,9,10]都不可能,结果的排序方式就无关紧要。 So far I have a piece of code that does what I want but I'd like the result to be in list format instead of tuples.
到目前为止,我有一段代码可以满足我的要求,但是我希望结果以列表格式而不是元组的形式出现。 It uses the unordered_tuples function from the sage library but all that does is return a list of lists like so:
它使用了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]]
And: 和:
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
Output: 输出:
[((([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])]
Want: 想:
[[[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]]]
output = [[[tuple[0]], [0], [tuple[1]], [0]] for tuple in unordered_tuples([10,9,8],2)]
I ran into a similar issue when extracting data from a PostgreSQL database. 从PostgreSQL数据库提取数据时遇到了类似的问题。 This is the method I used.
这是我使用的方法。
def tupleToList(t):
l = []
for i in xrange(len(t)):
l.append(list(t[i]))
pass
return l
pass
This works for 2D lists. 这适用于2D列表。 I'm working on implimenting it to 5D lists in your case.
在您的情况下,我正在努力将其添加到5D列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.