[英]How can I write a code that will return the answers from a dictionary?
所以我制作了两个单独的字典, 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
所以,现在我想创建一个新的 function 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
]
output 必须通过此 go
[
"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}
]
所以 output 应该将此作为字典返回...
["Pork Stew":{22,295}, "Green Salad1": {690}, "T-bone" : {405}]
老实说,我不知道如何做到这一点......我有所有必要的数据,我创建了辅助函数来获取数据,但我似乎无法得到这部分。
在不使用 import、collections.iter 或类似 lamda 的情况下编写此代码的最简单方法是什么?
我的版本:
拆分食谱与您的版本相似,但我使用的名称意味着某些东西 - 因此阅读代码更简单。
我假设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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.