所以我制作了兩個單獨的詞典,rdict(recipes)其中包含食譜,以及totalcal(data)包含每種成分的卡路里。我了解到在此任務中使用字典會很有幫助,因此在許多 StackOverflow 用戶的幫助下,我已經制定了這樣的代碼......
def rdict(recipes):
d = dict()
for r in recipes:
i = dict()
r_ = r.split(':')
for c_ in r_[1].split(','):
i_ = c_.split('*')
i[i_[0].strip()] = int(i_[1])
d[r_[0]] = i
return d
def totalcal(data):
calorie_dict = {}
for el in data:
food, values = el.split(':')
a, b, c = values.split(',')
calories = (int(a) * 5) (int(b) * 5) (int(c) * 9)
calorie_dict[food] = calories
return calorie_dict
所以,現在我想創建一個新函式gathercal(recipes, data)來回傳消耗的總卡路里。
例如,gathercal(recipes, data)如果
recipes = [[
"Pork Stew:Cabbage*5,Carrot*1,Fatty Pork*10",
"Green Salad1:Cabbage*10,Carrot*2,Pineapple*5",
"T-Bone:Carrot*2,Steak Meat*1"
]]
和
data = [
'Cabbage': 30,
'Carrot': 95,
'Fatty Pork': 2205,
'Pineapple': 40,
'Steak Meat': 215,
'Rabbit Meat': 225
]
輸出必須經過這個
[
"Pork Stew" : {(30 * 5) (95 * 1) (2205 * 10) = 22,295},
"Green Salad1" : {(30 * 10) (95 * 2) (40 * 5) = 690},
"T-bone" : {(95 * 2) (215 * 1) = 405}
]
所以輸出應該將它作為字典回傳......
["Pork Stew":{22,295}, "Green Salad1": {690}, "T-bone" : {405}]
老實說,我不知道如何做到這一點......我擁有所有必要的資料,并創建了輔助函式來獲取資料,但我似乎無法獲得這部分。
在不使用 import、collections.iter 或 lamda 之類的情況下撰寫此代碼的最簡單方法是什么?
uj5u.com熱心網友回復:
我的版本:
拆分食譜與您的版本類似,但我使用的名稱意味著某些東西 - 因此閱讀代碼更簡單。
我認為這data不需要拆分。
recipes = [
"Pork Stew:Cabbage*5,Carrot*1,Fatty Pork*10",
"Green Salad1:Cabbage*10,Carrot*2,Pineapple*5",
"T-Bone:Carrot*2,Steak Meat*1"
]
data = {
'Cabbage': 30,
'Carrot': 95,
'Fatty Pork': 2205,
'Pineapple': 40,
'Steak Meat': 215,
'Rabbit Meat': 225
}
# --- split recipes ---
recipes_splitted = {}
for r in recipes:
recipe_name, parts = r.split(":")
recipe_parts = {}
for part in parts.split(','):
product, number = part.split('*')
recipe_parts[product] = int(number)
recipes_splitted[recipe_name] = recipe_parts
# --- display recipes_splitted ---
print('\n--- recipes_splitted ---\n')
#for recipe_name, recipe_parts in recipes_splitted.items():
for recipe_name in recipes_splitted:
recipe_parts = recipes_splitted[recipe_name]
print(recipe_name, recipe_parts)
# --- calculate calories ---
print('\n--- calculate calories ---\n')
recipes_calories = {}
#for recipe_name, recipe_parts in recipes_splitted.items():
for recipe_name in recipes_splitted:
recipe_parts = recipes_splitted[recipe_name]
print('---', recipe_name, '---')
calories = 0
#for product, number in recipe_parts.items():
for product in recipe_parts:
number = recipe_parts[product]
print(product, ':', number)
calories = number * data[product]
print('>>> calories =', calories)
recipes_calories[recipe_name] = calories
print()
print(recipes_calories)
# --- display recipes_calories---
print('\n--- recipes_calories ---\n')
#for recipe_name, calories in recipes_calories.items():
for recipe_name in recipes_calories:
calories = recipes_calories[recipe_name]
print(recipe_name, ":", calories)
結果:
--- recipes_splitted ---
Pork Stew {'Cabbage': 5, 'Carrot': 1, 'Fatty Pork': 10}
Green Salad1 {'Cabbage': 10, 'Carrot': 2, 'Pineapple': 5}
T-Bone {'Carrot': 2, 'Steak Meat': 1}
--- calculate calories ---
--- Pork Stew ---
Cabbage : 5
Carrot : 1
Fatty Pork : 10
>>> calories = 22295
--- Green Salad1 ---
Cabbage : 10
Carrot : 2
Pineapple : 5
>>> calories = 690
--- T-Bone ---
Carrot : 2
Steak Meat : 1
>>> calories = 405
{'Pork Stew': 22295, 'Green Salad1': 690, 'T-Bone': 405}
--- recipes_calories ---
Pork Stew : 22295
Green Salad1 : 690
T-Bone : 405
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/363155.html
上一篇:為什么我的函式沒有被完全列印?
