例如,我有如下所示的对象列表:
[[{1},{2},{3}],[{4},{5}],[{6},{7},{8}]]
我需要对它们进行迭代,以获得每个迭代对象,如:
1,4,6
1,4,7
1,4,8
1,5,6
1,5,7
1,5,8
2,4,6
2,4,7
2,4,8
2,5,6
2,5,7
2,5,8
基本上,每个结果都类似于输入列表的子数组。
发布于 2016-05-18 03:46:41
您可以很容易地使用itertools.product
>>> import itertools
>>> x = list(itertools.product([1,2,3],[4,5],[6,7,8]))
[(1, 4, 6), (1, 4, 7), (1, 4, 8), (1, 5, 6), (1, 5, 7), (1, 5, 8), (2, 4, 6), (2, 4, 7), (2, 4, 8), (2, 5, 6), (2, 5, 7), (2, 5, 8), (3, 4, 6), (3, 4, 7), (3, 4, 8), (3, 5, 6), (3, 5, 7), (3, 5, 8)]
注意,您要寻找的每个组合的输出都称为输入列表的笛卡尔积。
https://stackoverflow.com/questions/37298611
复制相似问题