如何获取嵌套列表python中的项目

时间:2021-06-19 15:58:12

标签: python nested-lists

我正在查看此列表:

    'In-N-Out Burger': 
        {'Burgers': 
             {'Cheeseburger - Protein® Style (Bun replaced with Lettuce) ': {'calories': 330,'carbs': 11, 'fat': 25},
              'Hamburger w/Onion ': {'calories': 390, 'carbs': 39, 'fat': 19},
              'Double-Double® w/Onion': {'calories': 670, 'carbs': 39, 'fat': 41},
              'Cheeseburger w/Mustard & Ketchup Instead of Spread': {'calories': 400, 'carbs': 41,'fat': 18},
              'Double-Double® w/Mustard & Ketchup Instead of Spread': {'calories': 590, 'carbs': 41, 'fat': 32},
              'Double-Double® - Protein® Style (Bun replaced with Lettuce) ': {'calories': 520, 'carbs': 11, 'fat': 39},
              'Hamburger - Protein® Style (Bun replaced with Lettuce) ': {'calories': 240, 'carbs': 11, 'fat': 17},
              'Hamburger w/Mustard & Ketchup Instead of Spread': {'calories': 310, 'carbs': 41, 'fat': 10},
              'Cheeseburger w/Onion ': {'calories': 480, 'carbs': 39, 'fat': 27}},
         'French Fries':
             {'French Fries ': {'calories': 395, 'carbs': 54, 'fat': 18}},
         'Shakes':
             {'Chocolate Shake ': {'calories': 590, 'carbs': 72, 'fat': 29},
              'Strawberry Shake ': {'calories': 590, 'carbs': 81, 'fat': 27},
              'Vanilla Shake ': {'calories': 580, 'carbs': 67, 'fat': 31}}},
    'Five Guys Burgers and Fries':
        {'Sandwiches':
            {'Grilled Cheese': {'calories': 470,'carbs': 41, 'fat': 26},
             'Veggie Sandwich': {'calories': 440, 'carbs': 60, 'fat': 15}},
         'Burgers': {'Little Bacon Burger': {'calories': 560, 'carbs': 39, 'fat': 33},
                     'Hamburger': {'calories': 700, 'carbs': 39, 'fat': 43},
                     'Little Hamburger': {'calories': 480, 'carbs': 39, 'fat': 26},
                     'Bacon Burger': {'calories': 780, 'carbs': 39, 'fat': 50},
                     'Cheeseburger': {'calories': 840, 'carbs': 40, 'fat': 55},
                     'Bacon Cheeseburger': {'calories': 920, 'carbs': 40, 'fat': 62}},
         'Entrees': {'Bacon Dog': {'calories': 625, 'carbs': 40, 'fat': 42},
                     'Hot Dog': {'calories': 545, 'carbs': 40, 'fat': 35}},
         'French Fries': {'One Serving of Fries approx half of regular order': {'calories': 310, 'carbs': 39, 'fat': 15},
                          'Regular Fries': {'calories': 620, 'carbs': 78, 'fat': 30},
                          'Large Fries': {'calories': 1474, 'carbs': 184, 'fat': 71}}}}

我刚开始学习 Python,似乎无法弄清楚如何访问嵌套在列表中的信息。我的最终目标是定义一个函数,该函数可以使用每个类别中的一个项目创建尽可能高热量的膳食。

我正在使用

rivals['In-N-Out Burger']['Burgers'] 
no_of_burgers = len(rivals['In-N-Out Burger']['Burgers'])
no_of_burgers 

获取列表中的总数,我知道

rivals['In-N-Out Burger']['Burgers']['Cheeseburger - Protein® Style (Bun replaced with Lettuce) ']

生成该嵌套项中列出的内容的输出。我需要一些关于创建一个简单函数来定义每个类别中热量最高的膳食的建议。任何帮助将不胜感激,以及对 python 新手的任何指示

1 个答案:

答案 0 :(得分:1)

请注意,您的数据不包含 list(由方括号 [] 表示)您只有 dict(键/值)


对于包含同一餐的多个项目的字典,您可以将 max 与专用的 key 一起使用,该 'calories' 将检索 {'Chocolate Shake ': {'calories': 590, 'carbs': 72, 'fat': 29}, 'Strawberry Shake ': {'calories': 590, 'carbs': 81, 'fat': 27}, 'Vanilla Shake ': {'calories': 580, 'carbs': 67, 'fat': 31}}} def max_calories_item(items: Dict[str, Dict[str, int]]): return max(items.items(), key=lambda x: x[1]['calories'])

for restaurant, categories in values.items():
    total_cal = 0
    menu = []
    for name, meals in categories.items():
        most_cal = max_calories_item(meals)
        total_cal += most_cal[1]['calories']
        menu.append(most_cal[0])

    print(f"At {restaurant} the highest calorie-dense meal is composed of {menu} for a total of {total_cal}cals")


At In-N-Out Burger the highest calorie-dense meal is composed of ['Double-Double® w/Onion', 'French Fries ', 'Chocolate Shake '] for a total of 1655cals
At Five Guys Burgers and Fries the highest calorie-dense meal is composed of ['Grilled Cheese', 'Bacon Cheeseburger', 'Bacon Dog', 'Large Fries'] for a total of 3489cals

然后你可以迭代你的数据集,并为每家餐厅计算最高卡路里的餐

hidden
相关问题