如何根据元素组合的规范生成列表 [英] How to generate lists from a specification of element combinations

查看:85
本文介绍了如何根据元素组合的规范生成列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以如下形式指定的元素组合生成一堆列表:

I want to generate a bunch of lists using combinations of elements specified in a form like the following:

[[10, 20], [30, 40], [50, 60]]

这意味着第一个元素可用的值是10和20,第二个元素可用的值是30和40,依此类推(为简洁起见,我仅对每个元素使用了两个元素选项;为了简单起见,比那更多的).我想使用此规范通过这些元素的组合来生成所有列表(包括没有任何元素的可能性),生成类似以下内容的东西:

This means that the values available for the first element are 10 and 20, the values available for the second element are 30 and 40 and so on (I've used just two element options for each element for brevity; there could be more than that). I want to use this specification to generate all lists using the combinations of these elements (including the possibility of not having any), generating something like the following:

[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可以用于此目的,但是我不确定如何实现生成这样的列表的算法.像我上面显示的那样,从规范生成列表的一种好的通用方法(例如,不限于三个硬编码嵌套循环的三个元素)?

I feel as though itertools could be used for this, but I'm not sure how to implement an algorithm to generate lists like this. What would be a good, general way (i.e. not limited to three elements by three hardcoded nested loops, for instance) to generate lists from a specification like that which I've shown above?

作为尝试,我得到了以下信息:

As an attempt, I've got the following:

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 元素而产生的可能性:

This produces the following lists, but note that it misses out on the possibilities that arise from having no element:

[10, 30, 50]
[10, 30, 60]
[10, 40, 50]
[10, 40, 60]
[20, 30, 50]
[20, 30, 60]
[20, 40, 50]
[20, 40, 60]


我已经提出了以下建议,但对我来说似乎很微不足道:


I've come up with the following, but it seems very inelegant to me:

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)

推荐答案

您可以根据找到的解决方案创建双循环列表理解:

You can create a double-for-loop list comprehension based on the solution you found:

>>> 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也许更干净一些:

Or maybe a bit cleaner, using enumerate:

>>> [x for i, _ in enumerate(elements) for x in itertools.product(*elements[:i+1])]

这篇关于如何根据元素组合的规范生成列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆