Find duplicates of dictionary in a list and combine them in Python

别说谁变了你拦得住时间么 提交于 2021-02-05 08:23:11

问题


I have this list of dictionaries:

"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",
            },
        ]

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",
        },
    ]

How do I go about it?


回答1:


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",
        },
    ]
}

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)

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.



来源:https://stackoverflow.com/questions/63196902/find-duplicates-of-dictionary-in-a-list-and-combine-them-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!