首页 > 解决方案 > Python - 将字典值列表减少为更小的列表

问题描述

我有一本字典,其 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

我正在使用替换和相似性比率,因为成分名称不像我的示例那样干净;例如,我可以将鸡蛋或煮鸡蛋作为一种成分。

谢谢你。

标签: pythondictionarybig-o

解决方案


使用集合而不是列表怎么样,因为每个食谱都有独特的成分并且顺序并不重要?

集合可以在 O(1) 恒定时间内搜索,而列表可以在 O(n) 时间内搜索。

下面是一些例子

例如:

recipe_dictionary = { 
    134: set(['salt', 'chicken', 'tomato paste canned']),
    523: set(['toast whole grain', 'feta cheese' 'egg', 'salt']), 
    12: set(['chicken', 'rice', 'parsley'])
}

non_repeatable_ingredients = set(['egg', 'chicken', 'beef'])

您可以像这样测试一个元素在集合中的存在:

for ingredient in recipe_dictionary[134]:
    if ingredient in non_repeatable_ingredients:
        # do something

推荐阅读