itertools具有多个元素的所有可能组合

时间:2018-08-30 16:02:54

标签: python permutation itertools

我正在尝试找到一种使用以下类别生成所有可能组合的方法。我需要每个Type, Cutlery, IsWeekend中的一个元素,以及至少FoodTypes中的一个元素。在这些参数中,我想生成所有可能的选择组合。

options = {
'Type' : ['Breakfast', 'Brunch', 'Lunch', 'Dinner'],
'Cutlery': ['Knife', 'Fork'],
'IsWeekend' : ['True', 'False'],
'FoodTypes' : ['Sausage', 'Bacon', 'Eggs', 'Toast']
}

例如。

Breakfast, Knife, True, Sausage
Breakfast, Knife, True, Sausage, Bacon
....
Breakfast, Fork, False, Sasuage, Eggs, Toast

我一直在使用itertoolsproduct来玩combinations,但是我似乎找不到正确的配方。它目前正在生成产品列表,但是每个列表中仅使用一个元素,例如:

Breakfast, Knife, True, Sausage
Breakfast, Knife, True, Bacon

如何将其扩展到涵盖多种食物选择?

2 个答案:

答案 0 :(得分:2)

您可以像这样使用itertools.combinations进行此操作:

from itertools import combinations

options = {
    'Type':       ['Breakfast', 'Brunch', 'Lunch', 'Dinner'],
    'Cutlery':    ['Knife', 'Fork'],
    'IsWeekend' : ['True', 'False'],
    'FoodTypes' : ['Sausage', 'Bacon', 'Eggs', 'Toast']
}

# get a list of all the dictionary's values
lst = [j for i in options.values() for j in i]

for i in range(len(lst)+1):
    for s in combinations(lst, i): print(s)

注意::此解决方案将计算字典值的扁平列表的所有2 ^ 12组合,并假设字典的值中不包含重复项。如果有重复项,则在输入for循环之前,您可以轻松地将其删除,例如使用sets

答案 1 :(得分:2)

从您的示例中可以看出,您需要食物组的幂集,然后是具有该条目的每个其他条目的所有组合。我包括Python powerset文档中提供的itertools

from itertools import *

def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

options = {
    'Type' : ['Breakfast', 'Brunch', 'Lunch', 'Dinner'],
    'Cutlery': ['Knife', 'Fork'],
    'IsWeekend' : ['True', 'False'],
    'FoodTypes' : ['Sausage', 'Bacon', 'Eggs', 'Toast']
}

menu = powerset(options['FoodTypes'])

for setting in product(
    options['Type'],
    options['Cutlery'],
    options['IsWeekend'],
    menu ):

    print(setting)

输出如下;我相信您可以整理列表并删除空白菜单项(即,作为练习供读者阅读)。

('Breakfast', 'Knife', 'True', ())
('Breakfast', 'Knife', 'True', ('Sausage',))
('Breakfast', 'Knife', 'True', ('Bacon',))
('Breakfast', 'Knife', 'True', ('Eggs',))
('Breakfast', 'Knife', 'True', ('Toast',))
('Breakfast', 'Knife', 'True', ('Sausage', 'Bacon'))
('Breakfast', 'Knife', 'True', ('Sausage', 'Eggs'))
('Breakfast', 'Knife', 'True', ('Sausage', 'Toast'))
('Breakfast', 'Knife', 'True', ('Bacon', 'Eggs'))
('Breakfast', 'Knife', 'True', ('Bacon', 'Toast'))
('Breakfast', 'Knife', 'True', ('Eggs', 'Toast'))
('Breakfast', 'Knife', 'True', ('Sausage', 'Bacon', 'Eggs'))
('Breakfast', 'Knife', 'True', ('Sausage', 'Bacon', 'Toast'))
('Breakfast', 'Knife', 'True', ('Sausage', 'Eggs', 'Toast'))
('Breakfast', 'Knife', 'True', ('Bacon', 'Eggs', 'Toast'))
('Breakfast', 'Knife', 'True', ('Sausage', 'Bacon', 'Eggs', 'Toast'))
('Breakfast', 'Knife', 'False', ())
('Breakfast', 'Knife', 'False', ('Sausage',))
('Breakfast', 'Knife', 'False', ('Bacon',))
('Breakfast', 'Knife', 'False', ('Eggs',))
('Breakfast', 'Knife', 'False', ('Toast',))
...
('Dinner', 'Fork', 'False', ('Sausage', 'Eggs', 'Toast'))
('Dinner', 'Fork', 'False', ('Bacon', 'Eggs', 'Toast'))
('Dinner', 'Fork', 'False', ('Sausage', 'Bacon', 'Eggs', 'Toast'))