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
我正在使用替换和相似性比率,因为成分名称不像我的示例那样干净;例如,我可以将鸡蛋或煮鸡蛋作为一种成分。
谢谢你。
解决方案
使用集合而不是列表怎么样,因为每个食谱都有独特的成分并且顺序并不重要?
集合可以在 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
推荐阅读
- javascript - JavaScript 显示/隐藏 div
- r - Drop levels of the same name from the whole dataframe
- facebook-graph-api - 使用 GraphAPI 发布到 Facebook 页面
- angularjs - @angular/cli@1.7.4 - 未捕获的类型错误:无法读取未定义的属性“模块”
- algorithm - 包含引用插入的 YAML 缩小算法
- python-3.x - 如何找到特定变量的最高 R 平方值(在 python 中)?
- forms - Drupal 8 联系表格不要在电子邮件中发送消息
- knex.js - 节点.js | knex:找不到命令
- firebase - Firebase function (node.js) to return download url of image (via filename) from storage
- python - scipy优化leastsq 2D参数进行优化