创建一个字典列表,遍历输入值列表

cngwdvgl  于 2021-07-13  发布在  Java
关注(0)|答案(3)|浏览(326)

我正在尝试创建一个基于不同输入列表的字典列表。
示例:输入

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'}]

基本上,在最后我试图得到一个列表,其中包含输入中提供的列表(参数)的所有排列。

busg9geu

busg9geu1#

你只需要遍历你的列表,并将可能性附加到你的列表中 dinner_options 数组。

appetizer = ['soup', 'salad']
entree = ['steak', 'chicken', 'fish']
dessert = ['ice cream', 'flan', 'cake']

dinner_options = []

for ap in appetizer:
    for en in entree:
        for de in dessert:
            dinner_options.append({ "appetizer": ap, "entree": en, "dessert": de })

print(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'}]
62lalag4

62lalag42#

您可以使用itertools.product:

from itertools import product

appetizer = ['soup', 'salad']
entree = ['steak', 'chicken', 'fish']
dessert = ['ice cream', 'flan', 'cake']

# Here we create an iterable from the objects we want to obtain

# different combinations.

to_combine = (appetizer, entree, dessert)

# Next, we need to define the order in which those appear

# as those will be used as the keys in the individual

# dictionary objects.

order = 'appetizer,entree,dessert'.split(',')
dinner_options = list()

# Now we loop over the unpacked tuple and build each individual

# dictionary.

for items in product(*to_combine):
    # This creates the individual objects which are of the form:
    # `{'appetizer': '<a>', 'entree': '<e>', 'dessert': '<d>'}
    d = {key:value for key, value in zip(order, items)}
    dinner_options.append(d)

这将产生所需的输出。

n8ghc7c1

n8ghc7c13#

我认为这是最明显和最耗时的答案

appetizer = ['soup', 'salad']
entree = ['steak', 'chicken', 'fish']
dessert = ['ice cream', 'flan', 'cake']

# dict = {appetizer = [], entree = [], dessert = []}

dinner_options = []

for start in appetizer:
    for mid in entree:
        for end in dessert:
            data = dict(appetizer=start, entree=mid, dessert=end)
            dinner_options.append(data)

print(dinner_options)

相关问题