python字典的值可以是列表吗_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

我使用替换和相似比,因为成分名称不是我的例子; 例如,我可以将鸡蛋或煮鸡蛋作为一种成分。

谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值