我正在查看以下列表:
'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新手的指导都将不胜感激
请注意,您的数据不包含
list
(用方括号表示)[]
,您只有dict
(键/值)对于包含同一顿饭的多个项目的dict,您可以使用
max
和专用的key
来检索'calories'
值然后你可以迭代你的数据集,为每一家餐厅计算最高热量的一餐
相关问题 更多 >
编程相关推荐