首页 > python > Python:如何从列表中获取所有已排序的选项

Python:如何从列表中获取所有已排序的选项 (Python: How to get all the ordered options from lists in)

2018-12-18 pythoniteration

问题

我有以下方法

a = [1, 11, 111]
b = [2, 22, 222]
c = [3, 33, 333]
list_of_lists = [a, b, c]
lists_with_the_i_elements = [0 for x in range(len(list_of_lists))]
for i in range(0, len(list_of_lists)):
    lists_with_the_i_elements[i] = [list_i[i] for list_i in list_of_lists]
result = list(itertools.product(lists_with_the_i_elements[0],lists_with_the_i_elements[1],lists_with_the_i_elements[2]))
print(result)

reuslt:

[(1, 11, 111),
(1, 11, 222), 
(1, 11, 333), 
(1, 22, 111), 
(1, 22, 222), 
(1, 22, 333), 
(1, 33, 111), 
(1, 33, 222), 
(1, 33, 333), 
(2, 11, 111), 
(2, 11, 222), 
(2, 11, 333),
(2, 22, 111), 
(2, 22, 222), 
(2, 22, 333), 
(2, 33, 111), 
(2, 33, 222), 
(2, 33, 333),
(3, 11, 111),
(3, 11, 222),
(3, 11, 333),
(3, 22, 111), 
(3, 22, 222), 
(3, 22, 333), 
(3, 33, 111), 
(3, 33, 222), 
(3, 33, 333)]

预期结果是:

[1,2,3]
[1,22,3]
[1,222,3]
[1,2,33]
[1,22,33]
[1,222,33]
[1,2,333]
[1,22,333]
[1,222,333]
[11,2,3]
[11,22,3]
[11,222,3]
[11,2,33]
[11,22,33]
[11,222,33]
[11,2,333]
[11,22,333]
[11,222,333]
...

我希望有一个函数,当它将收到list_of_lists它将返回以下输出:因此,我想有一个函数,将给我所有的列表组合,这是从输入中的每个列表中的成员选择时的值索引对应于输入中的列表索引。

另一个简单例子:

def combo(*args):
     #do something
...
combo([1],[2],[3])
===>[1,2,3]
combo([1],[2],[3,33])
===>[1,2,3],[1,2,33]

我查看了所有选项itertools,但没有找到任何解决方案。

解决方法

这样的事情可以帮助你:

list(itertools.product([1,11,111], [2, 22, 222], [3, 33, 333]))

问题

I have the following method

a = [1, 11, 111]
b = [2, 22, 222]
c = [3, 33, 333]
list_of_lists = [a, b, c]
lists_with_the_i_elements = [0 for x in range(len(list_of_lists))]
for i in range(0, len(list_of_lists)):
    lists_with_the_i_elements[i] = [list_i[i] for list_i in list_of_lists]
result = list(itertools.product(lists_with_the_i_elements[0],lists_with_the_i_elements[1],lists_with_the_i_elements[2]))
print(result)

reuslt:

[(1, 11, 111),
(1, 11, 222), 
(1, 11, 333), 
(1, 22, 111), 
(1, 22, 222), 
(1, 22, 333), 
(1, 33, 111), 
(1, 33, 222), 
(1, 33, 333), 
(2, 11, 111), 
(2, 11, 222), 
(2, 11, 333),
(2, 22, 111), 
(2, 22, 222), 
(2, 22, 333), 
(2, 33, 111), 
(2, 33, 222), 
(2, 33, 333),
(3, 11, 111),
(3, 11, 222),
(3, 11, 333),
(3, 22, 111), 
(3, 22, 222), 
(3, 22, 333), 
(3, 33, 111), 
(3, 33, 222), 
(3, 33, 333)]

expected result is:

[1,2,3]
[1,22,3]
[1,222,3]
[1,2,33]
[1,22,33]
[1,222,33]
[1,2,333]
[1,22,333]
[1,222,333]
[11,2,3]
[11,22,3]
[11,222,3]
[11,2,33]
[11,22,33]
[11,222,33]
[11,2,333]
[11,22,333]
[11,222,333]
...

I want to have a function that when it will receive list_of_lists it will return following output: Hence, I want to have a function that will give me all the listing combinations, which are selections of a member from each list in the input when the value index is corresponding to the list index in the input.

Another simple example:

def combo(*args):
     #do something
...
combo([1],[2],[3])
===>[1,2,3]
combo([1],[2],[3,33])
===>[1,2,3],[1,2,33]

I went over all the options in itertools and didn't find any solution.

解决方法

Something like this should help you:

list(itertools.product([1,11,111], [2, 22, 222], [3, 33, 333]))
相似信息