[英]How to get items within a nested list 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 新手的任何指示
請注意,您的數據不包含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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.