【问题标题】:Pull a single entry from nested list of dicts by key按键从嵌套的字典列表中提取单个条目
【发布时间】: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


    【解决方案1】:

    听起来你得到了字典的id,你想得到ingredients——在这种情况下,你会想要这样的东西:

    def get_ingredients(id):
        for recipe in recipes:
            if recipe['id'] == id:
                return recipe['ingredients']
    

    请注意,如果您向其传递一个列表中的任何字典都不存在的 id,这将返回 None

    【讨论】:

      【解决方案2】:

      您正在尝试访问列表中的字典。 假设 recipe 是您的数据集并且它在全局命名空间中,您可以这样做:

      def get_ingradients(id): 
      
          for recipe in recipes: 
              if recipe.get("id") == id: 
                  return recipe.get("ingredients")
      
      

      这将返回您传递的 id 的渐变列表。

      【讨论】:

      • 返回AttributeError: 'list' object has no attribute 'get'
      • 我以为你的字典里面有字典。请再次检查代码。 @LMGagne
      【解决方案3】:

      假设数据集是一个字典,而你想要的id来自id字段,你可以以id字段为key重建字典。

      dd = [
      {'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']}
      ]
      
      dnew = {x['id']:x['ingredients'] for x in dd}
      
      print(dnew[10259])
      

      输出

      ['romaine lettuce', 'black olives', 'grape tomatoes', 'garlic', 'pepper', 'purple onion', 'seasoning', 'garbanzo beans', 'feta cheese crumbles']
      

      【讨论】:

      • 这是一个字典列表,而不是字典的字典
      【解决方案4】:

      你可以试试:

      data_dict = {0: {'id': 10259, 'cuisine': 'greek', 'ingredients': ['romaine lettuce', 'black olives', 'grape tomatoes', 'garlic', 'pepper', 'purple onion', 'seasoning', 'garbanzo beans', 'feta cheese crumbles']},
      1: {'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']},
      2: {'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']}}
      
      recipe_id = 25693
      [x for x in data_dict.values() if x['id']==recipe_id][0]['ingredients']
      

      输出

      ['plain flour',
       'ground pepper',
       'salt',
       'tomatoes',
       'ground black pepper',
       'thyme',
       'eggs',
       'green tomatoes',
       'yellow corn meal',
       'milk',
       'vegetable oil']```
      

      【讨论】:

        猜你喜欢
        • 2014-12-06
        • 1970-01-01
        • 2021-05-10
        • 1970-01-01
        • 1970-01-01
        • 2016-05-15
        • 1970-01-01
        • 1970-01-01
        • 2020-01-21
        相关资源
        最近更新 更多