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

问题描述 投票:1回答:2

我正在尝试使用以下类别找到生成所有可能组合的方法。我需要每个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

我一直在玩itertools使用productcombinations,但我似乎无法得到正确的食谱。它目前正在生成产品列表,但每个列表只使用一个元素,例如:

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

如何扩展它以涵盖多种食物选择?

python permutation itertools
2个回答
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'))

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

© www.soinside.com 2019 - 2024. All rights reserved.