我的冰箱里有一本字典,里面有我可用的食物,我想列印我能做的食譜,還有如果我有足夠的東西我可以做的食譜(顯示缺失的物品數量),還列印那些有缺少成分,因此它是什么成分。我只是設法將它們全部展示出來。
fridge = {
"orange" : 5,
"citron" : 3,
"sel" : 100,
"sucre" : 50,
"farine" : 250,
"tomates" : 6,
"huile" : 100,
"pomme" : 1,
"lait" : 1,
"pois chiche" : 1,
}
recipes = {
"jus_de_fruit" : {
"orange" : 3,
"citron" : 1,
"pomme" : 2
},
"salad" : {
"tomates" : 4,
"huile" : 10,
"sel" : 3
},
"crepes" : {
"lait" : 1,
"farine" : 250,
"oeufs" : 2
},
"glace" : {
"bac a glace" : 1,
"coulis abricot" : 1,
"batonnet" : 1
}
}
recette_dispo=[]
counter = 0
for recipe, recipe_contents in recipes.items():
if all(elem in list(fridge.keys()) for elem in list(recipes[recipe].keys())):
if all(recipe_contents[elem] <= fridge[elem] for elem in recipe_contents):
print(f'\n \n **** nice! you can make this recipe : {recipe} ****')
counter = 1
recette_dispo=recipe
else :
print(f'You have all ingredients but not enough in quantity for: {recipe}, you need: {list(recipe_contents.items())}')
else :
print(f'\n\n Tu nas pas tous les ingrédients pour : {recipe}')
print(f' You need these ingredients in order to make it : {list(recipe_contents.items())}')
print("\n I can make in total", counter, "recipe(s) from the ingredients in my fridge:",recette_dispo)
輸出:
You have all ingredients but not enough in quantity for: jus_de_fruit, you need:[('orange', 3), ('citron', 1), ('pomme', 2)]
#here I would like instead only the missing numbers of ingredients
**** nice! you can make this recipe : salad ****
You don't have all ingredients for: crepes
You need these ingredients : [('lait', 1), ('farine', 250), ('oeufs', 2)]
#here I would like instead not all the ingredients of the recipe but only the ones missing in my fridge
Tu nas pas tous les ingrédients pour : glace
You need these ingredients: [('bac a glace', 1), ('coulis abricot', 1), ('batonnet', 1)]
I can make in total 1recipe(s) from the ingredients in my fridge: salade
uj5u.com熱心網友回復:
您可以使用此示例如何從冰箱中查找丟失的物品:
fridge = {
"orange": 5,
"citron": 3,
"sel": 100,
"sucre": 50,
"farine": 250,
"tomates": 6,
"huile": 100,
"pomme": 1,
"lait": 1,
"pois chiche": 1,
}
recipes = {
"jus_de_fruit": {"orange": 3, "citron": 1, "pomme": 2},
"salad": {"tomates": 4, "huile": 10, "sel": 3},
"crepes": {"lait": 1, "farine": 250, "oeufs": 2},
"glace": {"bac a glace": 1, "coulis abricot": 1, "batonnet": 1},
}
for recipe_name, ingredients in recipes.items():
print(f"Recipe: {recipe_name}")
if (missing := ingredients.keys() - fridge.keys()) :
print("Missing ingredients:", missing)
else:
common = {
k: fridge[k] - ingredients[k]
for k in ingredients.keys() & fridge.keys()
}
if all(v >= 0 for v in common.values()):
print("All ingredients are OK")
else:
for i, v in common.items():
if v < 0:
print(f"Ingredient {i}: missing {-v} items")
print("-" * 80)
印刷:
Recipe: jus_de_fruit
Ingredient pomme: missing 1 items
--------------------------------------------------------------------------------
Recipe: salad
All ingredients are OK
--------------------------------------------------------------------------------
Recipe: crepes
Missing ingredients: {'oeufs'}
--------------------------------------------------------------------------------
Recipe: glace
Missing ingredients: {'batonnet', 'coulis abricot', 'bac a glace'}
--------------------------------------------------------------------------------
uj5u.com熱心網友回復:
我試圖實作與幾年前類似的東西(用匯編語言),但作為采購代理(在前世)并沒有為我提供更新冰箱、香料架等庫存所需的勤奮程度。因為我正在使用我的成分。像番茄醬、沙拉醬、橄欖和泡菜片這樣的東西不適合數豆。庫存管理應針對食品儲藏室和深冰柜,應避免在冰箱層面進行。
這只是我的觀點,但如果我們考慮到這一點并更多地依賴人情味,我們可以通過使用使程式的主要機制更加優雅sets
:
frigo = {
"orange" : 5,
"citron" : 3,
"sel" : 100,
"sucre" : 50,
"farine" : 250,
"tomates" : 6,
"huile" : 100,
"pomme" : 1,
"lait" : 1,
"pois chiche" : 1,
}
recettes = {
"jus_de_fruit" : {
"orange" : 3,
"citron" : 1,
"pomme" : 2
},
"salad" : {
"tomates" : 4,
"huile" : 10,
"sel" : 3
},
"crepes" : {
"lait" : 1,
"farine" : 250,
"oeufs" : 2
},
"glace" : {
"bac a glace" : 1,
"coulis abricot" : 1,
"batonnet" : 1
}
}
frigoSet = set(frigo)
peutFaire = []
nePeutPasFaire = dict()
for recette in recettes:
recetteSet = set(recettes[recette])
recetteSet.difference_update(frigoSet)
if len(recetteSet) == 0:
peutFaire.append(recette)
else:
nePeutPasFaire.update({recette: list(recetteSet)})
print("Vous avez les ingrédients pour faire:")
for i in peutFaire:
print(" {}".format(i))
print("\nIl manque des ingrédients pour les recettes suivantes:")
for i in nePeutPasFaire:
print(" {}".format(i))
for j in nePeutPasFaire[i]:
print(" {}".format(j))
程式的輸出是:
Vous avez les ingrédients pour faire:
jus_de_fruit
salad
Il manque des ingrédients pour les recettes suivantes:
crepes
oeufs
glace
coulis abricot
bac a glace
batonnet
然后,您可以使用您對冰箱中物品的個人知識來確定您想要制作什么。畢竟,你今天早上可能決定不做那些薄餅,因為你想把剩下的幾個雞蛋留著明天做肉餅。
這只是為了讓您了解如何使用集合來獲取基本資訊,而無需從一開始就對每個食譜中的每種成分進行大量比較。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/480951.html