Python:获取组合列表的最有效方法?

Python : Most efficient way to get list of combinations?

我有一本字典:

diff_params = {0: [a, b], 
               1: [c, d]}

每个键都是一个setting_name,每个值都是一个特定的设置。我希望能够列出如下列表:

[[a, c], 
 [a, d],
 [b, c],
 [b, d]] 

当我有 3 或 4 或 5 个设置并且每个设置都有多个选项时,我无法弄清楚如何干净利落。

看来你更具体地寻找itertools.product() which can be used like this with unpacking:

from itertools import product

result = product(*diff_params.values())

如果你真的想要一个列表列表,你可能需要转换结果(如果你使用 Python 2,list 周围是无用的,因为 map() return列表):

result = list(map(list, result))