python检查字典中的值是否对应于另一个中的值

发布于 2025-01-22 05:21:33 字数 1173 浏览 3 评论 0原文

我正在寻找一种解决方案,该解决方案允许从冰箱中的成分中查看可以从给定列表中的食谱进行检查。 因此,“食谱”和“冰箱”都是字典。

我所拥有的脚本没有考虑到值,而仅考虑键。 我想找到一个解决方案,使我只能找到结果“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

懒猫 2025-01-29 05:21:34

您可以通过使用这两个命令来检查您拥有多少橙色

fridge["orange"]  # Awnser : 5 

以及需要多少薄饼来进行薄饼

recipes["crepes"]["lait"]  # Awnser : 400

,您应该能够进行所需的比较。

You can check how much oranges you have by using

fridge["orange"]  # Awnser : 5 

and how much you lait you need for doing crepes by using

recipes["crepes"]["lait"]  # Awnser : 400

using these two commands, you should be able to do the comparisions you want.

半葬歌 2025-01-29 05:21:34

只需迭代并比较(首先要查找匹配点,然后进行值)。
只是做:

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(recipe)

结果是:

salade

Simply iterate and compare (keys first to find a match then values).
Just do:

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(recipe)

The result is:

salade
南冥有猫 2025-01-29 05:21:34

collections.counter根据所需的确切逻辑实现比较,因此您可以通过为冰箱内容和每个食谱制作counter来使其变得非常简单,并进行比较:

>>> from collections import Counter
>>> for n, recipe in recipes.items():
...     if Counter(recipe) <= Counter(fridge):
...         print(n)
...
salade

如果由于某种原因需要在没有Counter的情况下实现它,那么在配方中迭代这些项目并将每个项目与冰箱中的数量进行比较非常简单(这是确切的counter(配方)&lt; =计数器(冰箱)也在幕后做同样的事情:

>>> for n, recipe in recipes.items():
...     if all(c <= fridge.get(i, 0) for i, c in recipe.items()):
...         print(n)
...
salade

collections.Counter implements comparisons according to the exact logic you want, so you can make this very simple by making Counters for the fridge contents and each recipe, and comparing them:

>>> from collections import Counter
>>> for n, recipe in recipes.items():
...     if Counter(recipe) <= Counter(fridge):
...         print(n)
...
salade

If you needed to implement it without Counter for some reason, it's pretty simple to iterate over the items in recipe and compare each to the quantity in the fridge (this is the exact same thing that Counter(recipe) <= Counter(fridge) is doing behind the scenes:

>>> for n, recipe in recipes.items():
...     if all(c <= fridge.get(i, 0) for i, c in recipe.items()):
...         print(n)
...
salade
贪了杯 2025-01-29 05:21:34
def in_fridge(item):
    return item[0] in fridge.keys() and item[1] <= fridge[item[0]]
    
    
def check_recipes(name):  
    for item in recipes[name].items():
        if not in_fridge(item):
            return False
    return True
    
for name in recipes.keys():
    print(check_recipes(name))
    

False
True
False
def in_fridge(item):
    return item[0] in fridge.keys() and item[1] <= fridge[item[0]]
    
    
def check_recipes(name):  
    for item in recipes[name].items():
        if not in_fridge(item):
            return False
    return True
    
for name in recipes.keys():
    print(check_recipes(name))
    

False
True
False
屋顶上的小猫咪 2025-01-29 05:21:33

使用您的输入字典冰箱配方这两种方法可能会解决您的问题:

def in_fridge(ingredients: dict, fridge_food: dict) -> bool:
    
    for ingredient in ingredients:

        if ingredient not in fridge_food:
            return False

        if ingredients[ingredient] > fridge_food[ingredient]:
            return False

    return True

def check_recipes(recipes: dict, fridge_food: dict) -> None:

    for recipe in recipes:

        if in_fridge(recipes[recipe], fridge_food):

            print(f'There are enough ingredients to make {recipe}. ')

与词典

if __name__ == '__main__':

    check_recipes(recipes, fridge)

输出一起使用它们:

There are enough ingredients to make salade. 

如果您愿意,请说您可以做的食谱列表,改用:

def check_recipes(recipes: dict, fridge_food: dict) -> list:

    ans = []
    for recipe in recipes:

        if in_fridge(recipes[recipe], fridge_food):

            ans.append({recipe: recipes[recipe]})

    return ans

然后输出为

[{'salade': {'tomates': 4, 'huile': 10, 'sel': 3}}]

Using your input dictionaries fridge and recipes these two methods may solve your issue:

def in_fridge(ingredients: dict, fridge_food: dict) -> bool:
    
    for ingredient in ingredients:

        if ingredient not in fridge_food:
            return False

        if ingredients[ingredient] > fridge_food[ingredient]:
            return False

    return True

def check_recipes(recipes: dict, fridge_food: dict) -> None:

    for recipe in recipes:

        if in_fridge(recipes[recipe], fridge_food):

            print(f'There are enough ingredients to make {recipe}. ')

Using them with your dictionnaries

if __name__ == '__main__':

    check_recipes(recipes, fridge)

outputs:

There are enough ingredients to make salade. 

If you want, lets say a list of the recipes you can do, use instead:

def check_recipes(recipes: dict, fridge_food: dict) -> list:

    ans = []
    for recipe in recipes:

        if in_fridge(recipes[recipe], fridge_food):

            ans.append({recipe: recipes[recipe]})

    return ans

Then the output is

[{'salade': {'tomates': 4, 'huile': 10, 'sel': 3}}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文