如何在嵌套列表中获取项目

2024-09-27 09:37:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在查看以下列表:

    '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新手的指导都将不胜感激


Tags: ofinstyleoutfatburgerdoubleprotein
1条回答
网友
1楼 · 发布于 2024-09-27 09:37:35

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


对于包含同一顿饭的多个项目的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

相关问题 更多 >

    热门问题