Home > Enterprise >  Python check if value in dictionary corresponds to value in another one
Python check if value in dictionary corresponds to value in another one

Time:04-19

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

CodePudding user response:

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}}]

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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
  • Related