所以我正在嘗試創建一個函式 def mealcal(data, recipes, menu ),它接受三個字串,如
menu = ["T-Bone", "T-Bone", "Green Salad1"]
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:4,2,0", "Carrot:9,1,5", "Fatty Pork:431,1,5",
"Pineapple:7,1,0", "Steak Meat:5,20,10", "Rabbit Meat:7,2,20"]
并回傳
[22295, 690, 405]
為了做到這一點,我創建了多個輔助函式來獲得結果,這里是代碼..
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
def rdict(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
return recipes_splitted
def extract(recipes, menu):
data = rdict(menu)
result = []
for r in recipes:
tmp = []
for key in data[r]:
tmp.append(f"{key}:{data[r][key]}")
final_string = ""
for i in range(len(tmp)):
if i < len(tmp) - 1:
final_string = tmp[i] ", "
else:
final_string = tmp[i]
result.append(final_string)
return result
def recipecal(recipes, data):
calories_list = []
for recipe_name in rdict(recipes):
recipe_parts = rdict(recipes)[recipe_name]
calories = 0
for product in recipe_parts:
number = recipe_parts[product]
calories = number * totalcal(data)[product]
calories_list.append(calories)
return calories_list
print(extract(['T-Bone', 'Green Salad1','T-Bone'],["Pork Stew:Cabbage*5,Carrot*1,Fatty Pork*10",
"Green Salad1:Cabbage*10,Carrot*2,Pineapple*5",
"T-Bone:Carrot*2,Steak Meat*1"]))
def mealcal(menu, recipes, data):
x = extract(recipes,menu)
return recipecal(x,data)
所以,現在的mealcal(menu, recipes, data)作業,我必須把投入recipes和menu進入我的輔助函式extract,因為函式摘錄將回傳類似這樣的輸出
['Carrot:2, Steak Meat:1', 'Cabbage:10, Carrot:2, Pineapple:5', 'Carrot:2, Steak Meat:1']
然后我必須使用這個輸出extract(recipes,menu)來放入食譜部分recipecal(recipes,data)
資料是一個單獨的字串,將在開始時輸入,它看起來像這樣
data = ["Cabbage:4,2,0", "Carrot:9,1,5", "Fatty Pork:431,1,5",
"Pineapple:7,1,0", "Steak Meat:5,20,10", "Rabbit Meat:7,2,20"]
此外,當將兩個輸入放入 recipecal(recipes,data) 時,該函式將回傳數字
[22295, 690, 405]
So now my issue is how can I program mealcal(menu, recipes, data) to put recipes and menu into extract(recipes, menu) and then take the output of extract(recipes, menu) and input it into the recipe portion of totalcal(recipes,data) and then return the output correctly??
Please use the simplest method of code possible. I'm not very strong in programming and I would like to understand the codes that I write. Thank you:)
uj5u.com熱心網友回復:
這里的作業代碼。
這部分是正確的
x = extract(recipes,menu)
return recipecal(x,data)
但是里面很少有錯誤extract()導致問題。
它正在使用:而不是*- 所以它正在創建product:number而不是product*number,
name:之前沒加product1*number1,product1*number1
休息是一樣的。
def rdict(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
return recipes_splitted
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
def extract(recipes, menu):
recipes = rdict(recipes)
result = []
for r in menu:
tmp = []
for key in recipes[r]:
tmp.append(f"{key}*{recipes[r][key]}")
final_string = f"{r}:"
for i in range(len(tmp)):
if i < len(tmp) - 1:
final_string = tmp[i] ","
else:
final_string = tmp[i]
result.append(final_string)
return result
def recipecal(recipes, data):
calories_list = []
for recipe_name in rdict(recipes):
recipe_parts = rdict(recipes)[recipe_name]
calories = 0
for product in recipe_parts:
number = recipe_parts[product]
calories = number * totalcal(data)[product]
calories_list.append(calories)
return calories_list
def mealcal(menu, recipes, data):
recipes = extract(recipes, menu)
return recipecal(recipes, data)
# OR
#return recipecal(extract(recipes, menu), data)
# --- main ---
data = [
"Cabbage:4,2,0",
"Carrot:9,1,5",
"Fatty Pork:431,1,5",
"Pineapple:7,1,0",
"Steak Meat:5,20,10",
"Rabbit Meat:7,2,20"
]
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"
]
#menu = ['T-Bone', 'Green Salad1', 'T-Bone']
menu = ['Pork Stew', 'Green Salad1', 'T-Bone']
result = mealcal(menu, recipes, data)
print(result)
menu = ['T-Bone', 'Green Salad1', 'T-Bone']
#menu = ['Pork Stew', 'Green Salad1', 'T-Bone']
result = []
for single_product in menu:
#print(single_product)
# `menu` was a list `[string, string, string]
# `single_product` is a `string` but `mealcal` needs list `[string]`
single_result = mealcal( [single_product] , recipes, data)
#print(single_result)
result = result single_result # [a] [b] gives [a, b]
print(result)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/362934.html
標籤:python list function dictionary
下一篇:在這個字典中找不到最低的數值
