python检查字典中的值是否对应于另一个中的值
我正在寻找一种解决方案,该解决方案允许从冰箱中的成分中查看可以从给定列表中的食谱进行检查。 因此,“食谱”和“冰箱”都是字典。
我所拥有的脚本没有考虑到值,而仅考虑键。 我想找到一个解决方案,使我只能找到结果“ salade”,因为此脚本还将考虑到值(配方中的值必须相等或冰箱中的值以下)。
fridge = {
"orange" : 5,
"citron" : 3,
"sel" : 100,
"sucre" : 50,
"farine" : 250,
"lait" : 200,
"oeufs" : 1,
"tomates" : 6,
"huile" : 100,
}
recipes = {
"jus_de_fruit" : {
"orange" : 3,
"citron" : 1,
"pomme" : 1
},
"salade" : {
"tomates" : 4,
"huile" : 10,
"sel" : 3
},
"crepes" : {
"lait" : 400,
"farine" : 250,
"oeufs" : 2
}
}
def in_fridge(item):
if item in dictionnaire_frigo:
return True
else:
return False
def check_recipes(name):
for item in recipes[name]:
item_in_fridge = in_fridge(item)
if item_in_fridge == False:
return False
return True
for name in recipes:
print(check_recipes(name))
输出
false true True
if check_recipes(name) == True: print(name)
Outputs
Salade和Crepe,
但我只想找到Salade,因为我的冰箱中没有足够的成分“ Lait”
I am looking for a solution that allows to check what recipes from a given list I can make from the ingredients I have in my fridge.
So both "recipes" and "fridge" are dictionaries.
The script I have does not take into account the values but only the keys.
I would like to find a solution that allows me to only find the result "salade" as this script would also take into account the values (values in recipes must be equal or under values in fridge).
fridge = {
"orange" : 5,
"citron" : 3,
"sel" : 100,
"sucre" : 50,
"farine" : 250,
"lait" : 200,
"oeufs" : 1,
"tomates" : 6,
"huile" : 100,
}
recipes = {
"jus_de_fruit" : {
"orange" : 3,
"citron" : 1,
"pomme" : 1
},
"salade" : {
"tomates" : 4,
"huile" : 10,
"sel" : 3
},
"crepes" : {
"lait" : 400,
"farine" : 250,
"oeufs" : 2
}
}
def in_fridge(item):
if item in dictionnaire_frigo:
return True
else:
return False
def check_recipes(name):
for item in recipes[name]:
item_in_fridge = in_fridge(item)
if item_in_fridge == False:
return False
return True
for name in recipes:
print(check_recipes(name))
outputs
false true true
if check_recipes(name) == True: print(name)
outputs
salade and crepe
but I want to find only salade as I don't have enough of the ingredient "lait" in my fridge and it should not output crepe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过使用这两个命令来检查您拥有多少橙色
以及需要多少薄饼来进行薄饼
,您应该能够进行所需的比较。
You can check how much oranges you have by using
and how much you lait you need for doing crepes by using
using these two commands, you should be able to do the comparisions you want.
只需迭代并比较(首先要查找匹配点,然后进行值)。
只是做:
结果是:
Simply iterate and compare (keys first to find a match then values).
Just do:
The result is:
collections.counter
根据所需的确切逻辑实现比较,因此您可以通过为冰箱内容和每个食谱制作counter
来使其变得非常简单,并进行比较:如果由于某种原因需要在没有
Counter
的情况下实现它,那么在配方
中迭代这些项目并将每个项目与冰箱中的数量进行比较非常简单(这是确切的counter(配方)< =计数器(冰箱)
也在幕后做同样的事情:collections.Counter
implements comparisons according to the exact logic you want, so you can make this very simple by makingCounter
s for the fridge contents and each recipe, and comparing them:If you needed to implement it without
Counter
for some reason, it's pretty simple to iterate over the items inrecipe
and compare each to the quantity in the fridge (this is the exact same thing thatCounter(recipe) <= Counter(fridge)
is doing behind the scenes:使用您的输入字典
冰箱
和配方
这两种方法可能会解决您的问题:与词典
输出一起使用它们:
如果您愿意,请说您可以做的食谱列表,改用:
然后输出为
Using your input dictionaries
fridge
andrecipes
these two methods may solve your issue:Using them with your dictionnaries
outputs:
If you want, lets say a list of the recipes you can do, use instead:
Then the output is