在列表中查找字典的重复项并将其合并到Python中 [英] Find duplicates of dictionary in a list and combine them in Python

查看:49
本文介绍了在列表中查找字典的重复项并将其合并到Python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个词典列表:

"ingredients": [
            {
                "unit_of_measurement": {"name": "Pound (Lb)", "id": 13},
                "quantity": "1/2",
                "ingredient": {"name": "Balsamic Vinegar", "id": 12},
            },
            {
                "unit_of_measurement": {"name": "Pound (Lb)", "id": 13},
                "quantity": "1/2",
                "ingredient": {"name": "Balsamic Vinegar", "id": 12},
            },
            {
                "unit_of_measurement": {"name": "Tablespoon", "id": 15},
                "ingredient": {"name": "Basil Leaves", "id": 14},
                "quantity": "3",
            },
        ]

我希望能够找到配料的重复项(按名称或ID).如果有重复项并且具有相同的unit_of_measurement,则将它们合并为一个词典,并相应地添加数量.因此,以上数据应返回:

I want to be able to find the duplicates of ingredients (by either name or id). If there are duplicates and have the same unit_of_measurement, combine them into one dictionary and add the quantity accordingly. So the above data should return:

[
        {
            "unit_of_measurement": {"name": "Pound (Lb)", "id": 13},
            "quantity": "1",
            "ingredient": {"name": "Balsamic Vinegar", "id": 12},
        },
        {
            "unit_of_measurement": {"name": "Tablespoon", "id": 15},
            "ingredient": {"name": "Basil Leaves", "id": 14},
            "quantity": "3",
        },
    ]

我该怎么办?

推荐答案

假设您有一个这样表示的字典:

Assuming you have a dictionary represented like this:

data = {
    "ingredients": [
        {
            "unit_of_measurement": {"name": "Pound (Lb)", "id": 13},
            "quantity": "1/2",
            "ingredient": {"name": "Balsamic Vinegar", "id": 12},
        },
        {
            "unit_of_measurement": {"name": "Pound (Lb)", "id": 13},
            "quantity": "1/2",
            "ingredient": {"name": "Balsamic Vinegar", "id": 12},
        },
        {
            "unit_of_measurement": {"name": "Tablespoon", "id": 15},
            "ingredient": {"name": "Basil Leaves", "id": 14},
            "quantity": "3",
        },
    ]
}

您可以使用 collections.defaultdict列表的 (名称,id)分组键对成分进行分组:

What you could do is use a collections.defaultdict of lists to group the ingredients by a (name, id) grouping key:

from collections import defaultdict

ingredient_groups = defaultdict(list)
for ingredient in data["ingredients"]:
    key = tuple(ingredient["ingredient"].items())
    ingredient_groups[key].append(ingredient)

然后,您可以遍历此 defaultdict 的分组值,并使用

Then you could go through the grouped values of this defaultdict, and calculate the sum of the fraction quantities using fractions.Fractions. For unit_of_measurement and ingredient, we could probably just use the first grouped values.

from fractions import Fraction

result = [
    {
        "unit_of_measurement": value[0]["unit_of_measurement"],
        "quantity": str(sum(Fraction(ingredient["quantity"]) for ingredient in value)),
        "ingredient": value[0]["ingredient"],
    }
    for value in ingredient_groups.values()
]

然后哪个会给你这个结果:

Which will then give you this result:

[{'ingredient': {'id': 12, 'name': 'Balsamic Vinegar'},
  'quantity': '1',
  'unit_of_measurement': {'id': 13, 'name': 'Pound (Lb)'}},
 {'ingredient': {'id': 14, 'name': 'Basil Leaves'},
  'quantity': '3',
  'unit_of_measurement': {'id': 15, 'name': 'Tablespoon'}}]

您可能需要修改上述内容,以说明具有不同单位或度量的成分,但这应该可以帮助您入门.

You'll probably need to amend the above to account for ingredients with different units or measurements, but this should get you started.

这篇关于在列表中查找字典的重复项并将其合并到Python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆