Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中将列表转换为嵌套字典时出现问题_Python_Python 3.x_List_Dictionary - Fatal编程技术网

在Python中将列表转换为嵌套字典时出现问题

在Python中将列表转换为嵌套字典时出现问题,python,python-3.x,list,dictionary,Python,Python 3.x,List,Dictionary,我对Python相当陌生,正在使用嵌套字典。 我从以下网页收到一份词典列表,这是一个例子: Recipes = [{'Ingredient': 'Pork Mince', 'Quantity': 1, 'Recipe': 'Chilli Tofu'},  {'Ingredient': 'Courgettes', 'Quantity': 2, 'Recipe': 'Chilli Tofu'},  {'Ingredient': 'Firm Tofu', 'Quantity':

我对Python相当陌生,正在使用嵌套字典。 我从以下网页收到一份词典列表,这是一个例子:

    Recipes = [{'Ingredient': 'Pork Mince', 'Quantity': 1, 'Recipe': 'Chilli Tofu'},
     {'Ingredient': 'Courgettes', 'Quantity': 2, 'Recipe': 'Chilli Tofu'},
     {'Ingredient': 'Firm Tofu', 'Quantity': 1, 'Recipe': 'Chilli Tofu'},
     {'Ingredient': 'Garlic', 'Quantity': 1, 'Recipe': 'Chilli Tofu'},
     {'Ingredient': 'Chilli Bean Paste', 'Quantity': 1, 'Recipe': 'Chilli Tofu'},
     {'Ingredient': 'Chillies', 'Quantity': 1, 'Recipe': 'Chilli Tofu'},
     {'Ingredient': 'Spring Onions', 'Quantity': 2, 'Recipe': 'Chilli Tofu'},
     {'Ingredient': 'Courgettes', 'Quantity': 2, 'Recipe': 'Prawn & Courgettes'},
     {'Ingredient': 'Prawns', 'Quantity': 1, 'Recipe': 'Prawn & Courgettes'},
     {'Ingredient': 'Onions', 'Quantity': 1, 'Recipe': 'Prawn & Courgettes'},
     {'Ingredient': 'Garlic', 'Quantity': 1, 'Recipe': 'Prawn & Courgettes'},
     {'Ingredient': 'Chillies', 'Quantity': 1, 'Recipe': 'Prawn & Courgettes'}]
我想将其格式化为一个嵌套字典,这样我就可以将字典的“配方”值作为键,将成分和数量作为子字典的一部分。所以看起来像这样

{'Chilli Tofu': {
     'Spring Onions': 2,
     'Pork Mince': 1,
     'Firm Tofu': 1,
     'Courgettes': 2,
     'Garlic': 1,
     'Chilli Bean Paste': 1,
     'Chillies':1,
     'Spring Onions': 2
}, 
'Prawn & Courgettes': {
     'Chillies': 1,
     'Courgettes': 2,
      etc...
}}
我可以通过以下代码实现部分目标:

format_recipes = {}
for r in recipes:
    format_recipes[r['Recipe']] = {r['Ingredient']: r['Quantity']}
但是,当我运行上述操作时,我只能得到一本包含以下内容的词典:

{'Chilli Tofu':{'Spring Onions':2},'Prawn&courgetes':{'Chillies':1}

似乎我的嵌套字典在循环中不断被覆盖,我只得到了该配方列表中的最后一种成分和数量

我不知道如何防止这种情况发生,并将其附加到key:value对列表中,而不是覆盖它们。有什么想法吗


干杯

问题在于,事实上,每次迭代都要替换配方,您需要将其添加到字典列表中:

首先,您需要为每个配方定义空列表,然后添加每个配料

解决方案:

format_recipes = {}

for r in recipes:
    format_recipes[r['Recipe']] = []

for r in recipes:
    format_recipes[r['Recipe']].append({r['Ingredient']: r['Quantity']})

每次执行以下操作时,都会覆盖字典值:

format_recipes[r['Recipe']] = {r['Ingredient']: r['Quantity']}
您可以将
update
setdefault
一起使用,这将设置初始嵌套dict:

format_recipes = {}
for item in Recipes:
    format_recipes.setdefault(item['Recipe'], {}).update({item['Ingredient']: item['Quantity']})
然后,配方的格式为:

{'Chilli Tofu': {'Pork Mince': 1,
  'Courgettes': 2,
  'Firm Tofu': 1,
  'Garlic': 1,
  'Chilli Bean Paste': 1,
  'Chillies': 1,
  'Spring Onions': 2},
 'Prawn & Courgettes': {'Courgettes': 2,
  'Prawns': 1,
  'Onions': 1,
  'Garlic': 1,
  'Chillies': 1}}

非常感谢。标记马克的答案为被接受,因为它使用更少的代码。你伤了我的心!哈哈