我有一个字典,其ID是食谱ID,值是成分列表:recipe_dictionary = { 134: ['salt', 'chicken', 'tomato paste canned'],
523, ['toast whole grain', 'feta cheese' 'egg', 'salt'],
12: ['chicken', 'rice', 'parsley']
我还有一个静态列表,其中包含我不想在白天重复的成分:non_repeatable_ingredients = ['egg', 'chicken', 'beef']
现在我浏览字典的每个值,然后遍历成分名称,将每个名称与non_repeatable_ingredients列表进行比较,然后创建共享单词列表。所以我的缩小字典看起来像:reduced_recipe_dictionary = { 134: ['chicken'],
523, ['egg'],
12: ['chicken']
这个过程需要很长时间,因为我的真实词典和成分列表很长。有没有比下面更快的方法呢?
这是get_reduced_meal_plans_dictionry方法:reduced_meal_plans_dictionary = {}
# For each recipe
for recipe in meal_plans_dictionary:
# Temp list for overlapp ingredients found for each recipe
overlapped_ingredients_list = []
# For each complete name of ingredient in the recipe
for ingredient_complete_name in meal_plans_dictionary[recipe]:
# Clean up the ingredient name as it sometimes involves comma, parentheses or spaces
ingredient_string = ingredient_complete_name.replace(',', '').replace('(', '').replace(')', '').lower().strip()
# Compare each ingredient name against the list of ingredients that shall not repeated in a day
for each in PROTEIN_TAGS:
# Compute the partial similarity
partial_similarity = fuzz.partial_ratio(ingredient_string, each.lower())
# If above 90, means one of the ingredients in the PROTEIN_TAGS exists in this recipe
if partial_similarity > 90:
# Make a list of such ingredients for this recipe
overlapped_ingredients_list.append(each.lower())
# Place the recipe ID as the key and the reduced overlapped list as the value
reduced_meal_plans_dictionary[recipe] = overlapped_ingredients_list
我使用替换和相似比,因为成分名称不是我的例子; 例如,我可以将鸡蛋或煮鸡蛋作为一种成分。
谢谢。