【发布时间】:2021-01-09 01:52:47
【问题描述】:
我有一个如下所示的数据集:
[{'id': 10259,
'cuisine': 'greek',
'ingredients': ['romaine lettuce',
'black olives',
'grape tomatoes',
'garlic',
'pepper',
'purple onion',
'seasoning',
'garbanzo beans',
'feta cheese crumbles']},
{'id': 25693,
'cuisine': 'southern_us',
'ingredients': ['plain flour',
'ground pepper',
'salt',
'tomatoes',
'ground black pepper',
'thyme',
'eggs',
'green tomatoes',
'yellow corn meal',
'milk',
'vegetable oil']},
{'id': 20130,
'cuisine': 'filipino',
'ingredients': ['eggs',
'pepper',
'salt',
'mayonaise',
'cooking oil',
'green chilies',
'grilled chicken breasts',
'garlic powder',
'yellow onion',
'soy sauce',
'butter',
'chicken livers']}]
我正在尝试编写一个函数来为 id == 某个数字的 dict 提取 ingredients 列表,但我无法完全解决。
一开始我试过
def get_ingredients(recipe):
for recipe in recipes:
return recipe['ingredients']
但这似乎返回了任何条目的成分index==recipe
如何轻松访问下一级字典?
【问题讨论】:
标签: python list dictionary nested