我正在尝试创建一个基于不同输入列表的字典列表。
示例:输入
appetizer = ['soup', 'salad']
entree = ['steak', 'chicken', fish']
dessert = ['ice cream', 'flan', cake']
dict = {appetizer = [], entree = [], dessert = []}
dinner_options = []
从这里开始,我将创建一个包含所有可能晚餐选项的词典列表
输出
dinner_options = [{appetizer = 'soup', entree = 'steak', dessert = 'ice cream'},
{appetizer = 'soup', entree = 'steak', dessert = 'flan'},
{appetizer = 'soup', entree = 'steak', dessert = 'cake'},
{appetizer = 'soup', entree = 'chicken', dessert = 'ice cream'},
{appetizer = 'soup', entree = 'chicken', dessert = 'flan'},
{appetizer = 'soup', entree = 'chicken', dessert = 'cake'},
{appetizer = 'soup', entree = 'fish', dessert = 'ice cream'},
{appetizer = 'soup', entree = 'fish', dessert = 'flan'},
{appetizer = 'soup', entree = 'fish', dessert = 'cake'},
{appetizer = 'salad', entree = 'steak', dessert = 'ice cream'},
{appetizer = 'salad', entree = 'steak', dessert = 'flan'},
{appetizer = 'salad', entree = 'steak', dessert = 'cake'},
{appetizer = 'salad', entree = 'chicken', dessert = 'ice cream'},
{appetizer = 'salad', entree = 'chicken', dessert = 'flan'},
{appetizer = 'salad', entree = 'chicken', dessert = 'cake'},
{appetizer = 'salad', entree = 'fish', dessert = 'ice cream'},
{appetizer = 'salad', entree = 'fish', dessert = 'flan'},
{appetizer = 'salad', entree = 'fish', dessert = 'cake'}]
基本上,在最后我试图得到一个列表,其中包含输入中提供的列表(参数)的所有排列。
3条答案
按热度按时间busg9geu1#
你只需要遍历你的列表,并将可能性附加到你的列表中
dinner_options
数组。然后它就会显示出来
62lalag42#
您可以使用itertools.product:
这将产生所需的输出。
n8ghc7c13#
我认为这是最明显和最耗时的答案