打印可用并显示两个词典中缺少的项目
我有一个词典,里面包含我的冰箱中的食物,我想打印我可以制作的食谱,但如果我有足够的物品(显示丢失的物品数量),也可以打印出可以制作的食谱成分缺失,因此是什么成分。 我只是设法向他们展示了所有人。
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
I have a dictionary containing my available food in my fridge and I want to print the recipes I can make but also the ones that I could make if I had enough items (showing the number of items missing) and also print the ones that have an ingredient missing and thus what ingredient it is.
I just managed to show them all.
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)
Output:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用此示例如何从冰箱中找到缺少的项目:
打印:
You can use this example how to find missing items from the fridge:
Prints:
我试图实施类似于今年前(用汇编语言)类似的东西,但是作为一名采购代理(在前世)并没有为我提供更新我的冰箱,香料架的库存所需的勤奋程度。当我使用成分时。番茄酱,沙拉调味料,橄榄和切成薄片的泡菜等事情并不能很好地适合豆类。库存管理应适用于储藏室和深冰柜,并应在冰箱一级避免。
这只是我的看法,但是如果我们考虑到这一点并更多地依靠人类的触摸,我们可以通过使用
sets :
该程序的输出是:
您可以 :您可以:您可以:您可以:您可以:您可以:您可以:您可以:然后,使用您的个人个人了解您在冰箱中所拥有的知识来确定要制作的东西。毕竟,您可能会决定今天早上不做这些薄饼,因为明天您想节省几个鸡蛋。
这只是为了让您了解如何使用集合来获取基本信息,而无需通过从一开始就对每种食谱中的每种成分进行了很多比较。
I tried to implement something similar to this years ago (in assembly language), but being a purchasing agent (in a previous life) did not provide me with the level of diligence required to update the inventory of my refrigerator, spice rack, etc. as I was using my ingredients. Things like ketchup, salad dressings, olives, and sliced pickles do not lend themselves well to bean-counting. Inventory management should be for pantries and deep freezers and should be avoided at the refrigerator level.
That's just my opinion, but if we take that into consideration and rely more upon the human touch we can make the primary mechanics of the program more elegant by using
sets
:The output of the program is:
You can then use your personal knowledge of what you have in your refrigerator to determine what you want to make. After all, you may decide not to make those crepes this morning because you want to save the few eggs you have for brouillés tomorrow instead.
That's just to give you an idea of how you can use sets to get the basic information without looping through a bunch of comparisons for every ingredient in every recipe from the outset.