如何根据元素组合规范生成列表
How to generate lists from a specification of element combinations
我想使用如下形式指定的元素组合生成一堆列表:
[[10, 20], [30, 40], [50, 60]]
这意味着第一个元素的可用值是 10 和 20,第二个元素的可用值是 30 和 40 等等(为简洁起见,我只为每个元素使用了两个元素选项;那里可能不止于此)。我想使用这个规范来生成所有使用这些元素的组合的列表(包括没有任何元素的可能性),生成如下内容:
[10]
[20]
[10, 30]
[10, 40]
[20, 30]
[20, 40]
[10, 30, 50]
[10, 30, 60]
[10, 40, 50]
[10, 40, 60]
[20, 30, 50]
[20, 30, 60]
[20, 40, 50]
[20, 40, 60]
我觉得好像 itertools
可以用于此,但我不确定如何实现算法来生成这样的列表。什么是好的、通用的方法(例如,不限于三个硬编码嵌套循环的三个元素)从我上面显示的规范生成列表?
作为尝试,我得到了以下内容:
import itertools
element_specifications = [[10, 20], [30, 40], [50, 60]]
lists = [list(list_configuration) for list_configuration in list(itertools.product(*element_specifications))]
for list_configuration in lists:
print(list_configuration)
这会生成以下列表,但请注意,它错过了因具有 no 元素而产生的可能性:
[10, 30, 50]
[10, 30, 60]
[10, 40, 50]
[10, 40, 60]
[20, 30, 50]
[20, 30, 60]
[20, 40, 50]
[20, 40, 60]
编辑:我想出了以下内容,但对我来说似乎很不雅:
import itertools
element_specifications = [[10, 20], [30, 40], [50, 60]]
lists = []
for length in range(1, len(element_specifications) + 1):
lists.extend([list(list_configuration) for list_configuration in list(itertools.product(*element_specifications[:length]))])
for list_configuration in lists:
print(list_configuration)
您可以根据找到的解决方案创建一个双循环列表理解:
>>> elements = [[10, 20], [30, 40], [50, 60]]
>>> [x for i in range(len(elements)) for x in itertools.product(*elements[:i+1])]
[(10,),
(20,),
(10, 30),
(10, 40),
(20, 30),
(20, 40),
(10, 30, 50),
(10, 30, 60),
(10, 40, 50),
(10, 40, 60),
(20, 30, 50),
(20, 30, 60),
(20, 40, 50),
(20, 40, 60)]
或者更简洁一些,使用 enumerate
:
>>> [x for i, _ in enumerate(elements) for x in itertools.product(*elements[:i+1])]
我想使用如下形式指定的元素组合生成一堆列表:
[[10, 20], [30, 40], [50, 60]]
这意味着第一个元素的可用值是 10 和 20,第二个元素的可用值是 30 和 40 等等(为简洁起见,我只为每个元素使用了两个元素选项;那里可能不止于此)。我想使用这个规范来生成所有使用这些元素的组合的列表(包括没有任何元素的可能性),生成如下内容:
[10]
[20]
[10, 30]
[10, 40]
[20, 30]
[20, 40]
[10, 30, 50]
[10, 30, 60]
[10, 40, 50]
[10, 40, 60]
[20, 30, 50]
[20, 30, 60]
[20, 40, 50]
[20, 40, 60]
我觉得好像 itertools
可以用于此,但我不确定如何实现算法来生成这样的列表。什么是好的、通用的方法(例如,不限于三个硬编码嵌套循环的三个元素)从我上面显示的规范生成列表?
作为尝试,我得到了以下内容:
import itertools
element_specifications = [[10, 20], [30, 40], [50, 60]]
lists = [list(list_configuration) for list_configuration in list(itertools.product(*element_specifications))]
for list_configuration in lists:
print(list_configuration)
这会生成以下列表,但请注意,它错过了因具有 no 元素而产生的可能性:
[10, 30, 50]
[10, 30, 60]
[10, 40, 50]
[10, 40, 60]
[20, 30, 50]
[20, 30, 60]
[20, 40, 50]
[20, 40, 60]
编辑:我想出了以下内容,但对我来说似乎很不雅:
import itertools
element_specifications = [[10, 20], [30, 40], [50, 60]]
lists = []
for length in range(1, len(element_specifications) + 1):
lists.extend([list(list_configuration) for list_configuration in list(itertools.product(*element_specifications[:length]))])
for list_configuration in lists:
print(list_configuration)
您可以根据找到的解决方案创建一个双循环列表理解:
>>> elements = [[10, 20], [30, 40], [50, 60]]
>>> [x for i in range(len(elements)) for x in itertools.product(*elements[:i+1])]
[(10,),
(20,),
(10, 30),
(10, 40),
(20, 30),
(20, 40),
(10, 30, 50),
(10, 30, 60),
(10, 40, 50),
(10, 40, 60),
(20, 30, 50),
(20, 30, 60),
(20, 40, 50),
(20, 40, 60)]
或者更简洁一些,使用 enumerate
:
>>> [x for i, _ in enumerate(elements) for x in itertools.product(*elements[:i+1])]