简体   繁体   English

python中嵌套列表的可能组合

[英]Possible combination of a nested list in python

If I have a list of lists and want to find all the possible combination from each different indices, how could I do that? 如果我有一个列表列表,并且想从每个不同的索引中找到所有可能的组合,我该怎么做?

For example: 例如:

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

I want to find 我想找到

all_possibility = [[1, 5, 9], [1, 8, 6], [4, 2, 9], [4, 8, 3], [7, 2, 6], [7, 5, 3]]

where 哪里

  • [1,5,9]: 1 is 1st element of [1, 2, 3], 5 is 2nd element of [4, 5, 6], 9 is 3rd element of [7, 8, 9]. [1,5,9]:1是[1,2,3]的第一个元素,5是[4,5,6]的第二个元素,9是[7,8,9]的第三个元素。

  • [1,8,6]: 1 is 1st element of [1, 2, 3], 8 is 2nd element of [7, 8, 9], 6 is 3rd element of [4, 5, 6]. [1,8,6]:1是[1,2,3]的第一元素,8是[7,8,9]的第二元素,6是[4,5,6]的第三元素。

and so on. 等等。

(Edited) Note: I would like the result to be in the same order as the original element of the list. (已编辑)注意:我希望结果的顺序与列表中原始元素的顺序相同。 [1, 8, 6] but not [1, 6, 8] because 8 is the 2nd element of [7, 8, 9]. [1,8,6]但不是[1,6,8],因为8是[7,8,9]的第二个元素。

What you're looking for is the Cartesian product, in Python itertools.product : 您正在寻找的是笛卡尔积,在Python itertools.product

>>> import itertools
>>> list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> all_possibility = list(itertools.product(*list_of_lists))
>>> print(all_possibility)
[(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8),
 (1, 6, 9), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7),
 (2, 6, 8), (2, 6, 9), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9),
 (3, 6, 7), (3, 6, 8), (3, 6, 9)]

If you want permutations based on the indices rather than the values, you can use itertools.combinations to get the possible indices, then use those indices to get the respective values from the sub-lists, like this: 如果要基于索引而不是值进行排列,则可以使用itertools.combinations获取可能的索引,然后使用这些索引从子列表中获取相应的值,如下所示:

>>> list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> length = 3
>>> all_indices = list(itertools.permutations(range(length), length))
>>> all_possibility = [[l[i] for l,i in zip(list_of_lists, indices)] for indices in all_indices]
>>> print(all_possibility)
[[1, 5, 9], [1, 6, 8], [2, 4, 9], [2, 6, 7], [3, 4, 8], [3, 5, 7]]

I have to consider the indices as well. 我还必须考虑指数。 For example, (1, 4, 7) is excluded because 1 , and 4 both are the 1st element from the list of the lists (from [1, 2, 3] and [4, 5, 6] ). 例如,排除(1、4、7 (1, 4, 7)是因为14都是列表列表中的第一个元素(来自[1, 2, 3][4, 5, 6] )。 And actually (1, 4, 7) all of them are from the first component of the nested list. 实际上(1, 4, 7)所有这些都来自嵌套列表的第一部分。 I need the cases with all the different indices. 我需要所有不同索引的案例。

So you actually just want to get the possible permutations of a “list selector” for each index in the output, ie these are what you are trying to get: 因此,您实际上只想为输出中的每个索引获取“列表选择器”的可能排列,即,这些是您想要获取的:

>>> list(itertools.permutations(range(3), 3))
[(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]

And once you have that, you just need to translate into your list_of_lists where you access each index from the specified sublist: 一旦有了,只需将其转换为list_of_lists从指定的子列表访问每个索引:

>>> [[list_of_lists[k][i] for i, k in enumerate(comb)] for comb in itertools.permutations(range(3), 3)]
[[1, 5, 9], [1, 8, 6], [4, 2, 9], [4, 8, 3], [7, 2, 6], [7, 5, 3]]

In a spirit of @poke's approach, here is the cases for number of elements differ than the number of the list in the lists. 本着@poke的精神,这里是元素数量与列表中列表数量不同的情况。 (Previously, there are 3 elements in individual list where 3 sub-lists in the list). (以前,单个列表中有3个元素,而列表中有3个子列表)。

list_of_lists = [[1, 2], [3, 4], [5, 6], [7, 8]]

We expect every (0, 1) pairs from the list of lists, or 我们希望列表中的每(0,1)对,或者

all_possibility = [[1, 4], [1, 6], [1, 8], [3, 2], [3, 6], [3, 8], \
                   [5, 2], [5, 4], [5, 8], [7, 2], [7, 4], [7, 6]]

The code: 编码:

permutation_cases = list(itertools.permutations(range(2), 2))
select_from = list(itertools.combinations(range(len(list_of_lists)), 2))

all_possibility = []
for selecting_index in select_from:
        selected = [list_of_lists[i] for i in selecting_index ]
        cases = list([selected[k][i] for i, k in enumerate(comb)] for comb in permutation_cases)
        for item in cases:
            all_possibility.append(item)
print(all_possibility)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM