Python:从两个字典列表中查找重复的字典值

kmpatx3s  于 2023-01-08  发布在  Python
关注(0)|答案(2)|浏览(130)

我想建立一个简单的python脚本,可以将多个食谱中的所有项目添加到一个excel文档中,使购物和坚持膳食计划变得更容易。我能够让一个简单的版本工作,然而,当它导出到excel时,如果多个食谱具有相同的成分,它将在excel文档中多次出现。我一直试图让它把这些成分的量加在一起,而不是重复。下面是两种配方成分的样本。

import pandas as pd

friedRiceShrimp = [
    {"Ingredient": "Garlic clove", "Size": "clove", "Quantity": 3, "Aisle": "Produce"},
    {"Ingredient": "Instant Brown Rice", "Size": "oz", "Quantity": 16, "Aisle": 22},
    {"Ingredient": "Soy Sauce", "Size": "tbsp", "Quantity": 2, "Aisle": 22},
    {"Ingredient": "Chili Paste", "Size": "tbsp", "Quantity": 2, "Aisle": 22},
    {"Ingredient": "Honey", "Size": "tbsp", "Quantity": 1, "Aisle": 18},
    {"Ingredient": "Peanut Oil", "Size": "tbsp", "Quantity": 2, "Aisle": 21},
    {"Ingredient": "Shrimp", "Size": "oz", "Quantity": 10, "Aisle": 12},
    {"Ingredient": "Egg", "Size": "individual", "Quantity": 3, "Aisle": "Dairy"},
    {"Ingredient": "Snap Peas", "Size": "cup", "Quantity": 1.5, "Aisle": "Produce"},
    {"Ingredient": "Peanuts Dry-Roasted", "Size": "cup", "Quantity": .3, "Aisle": 14}
]

macNCheese = [
    {"Ingredient": "Bacon", "Size": "Slice", "Quantity": 6, "Aisle": 8},
    {"Ingredient": "Chicken Stock", "Size": "cup", "Quantity": 2, "Aisle": 24},
    {"Ingredient": "Milk", "Size": "cup", "Quantity": 1, "Aisle": "Dairy"},
    {"Ingredient": "Butternut Squash puree", "Size": "oz", "Quantity": 10, "Aisle": "Produce"},
    {"Ingredient": "Macaroni Elbow Pasta", "Size": "oz", "Quantity": 10, "Aisle": 23},
    {"Ingredient": "Broccoli", "Size": "cup", "Quantity": 3, "Aisle": "Produce"},
    {"Ingredient": "Cheddar Cheese Shredded", "Size": "oz", "Quantity": 5, "Aisle": 8},
    {"Ingredient": "Garlic clove", "Size": "clove", "Quantity": 2, "Aisle": "Produce"}
]`

shopping_list = friedRiceShrimp + macNCheese
df = pd.DataFrame(data=shopping_list)
df.to_excel("Test.xlsx", index=False)

我尝试了下面的循环和几个不同的循环。我的想法是循环通过shopping_list,并将每一个项目添加到一个新的列表中。如果项目已经在新列表中(和“大小”是相同的)数量将加在一起,而不是有一个重复的项目显示在我的excel导出。我的问题是我只匹配成分在idx相比,一切在final_list。我也愿意并理解,我可能需要从头再做,使我的最终愿景工作。

shopping_list = friedRiceShrimp + macNCheese
final_list = []

for idx in shopping_list:
    if idx["Ingredient"] in final_list[0]["Ingredient"]: #needs to check if idx ingredent is already in list somehow
        final_list[0]["Quantity"] += idx["Quantity"] #if that idx ingredent in final list add just quantity
    else:
        final_list.append(idx)
        print(idx["Ingredient"])

谢谢你的帮助和帮助,我仍然在学习Python,并试图找到在日常生活中使用它的方法,以帮助巩固想法和概念。

hjqgdpho

hjqgdpho1#

实际上,您正在尝试合并/累计具有相同('Ingredient', 'Size')属性的重复记录,并对它们的Quantity求和。
使用pandas幂对所需列进行group by运算,并按需要计算或约简的其他列进行聚合。

shopping_list = friedRiceShrimp + macNCheese
df = pd.DataFrame(data=shopping_list)
df = df.groupby(['Ingredient', 'Size'], as_index=False).agg({'Quantity': 'sum', 'Aisle': 'first'})
print(df)
Ingredient        Size  Quantity    Aisle
0                     Bacon       Slice       6.0        8
1                  Broccoli         cup       3.0  Produce
2    Butternut Squash puree          oz      10.0  Produce
3   Cheddar Cheese Shredded          oz       5.0        8
4             Chicken Stock         cup       2.0       24
5               Chili Paste        tbsp       2.0       22
6                       Egg  individual       3.0    Dairy
7              Garlic clove       clove       5.0  Produce
8                     Honey        tbsp       1.0       18
9        Instant Brown Rice          oz      16.0       22
10     Macaroni Elbow Pasta          oz      10.0       23
11                     Milk         cup       1.0    Dairy
12               Peanut Oil        tbsp       2.0       21
13      Peanuts Dry-Roasted         cup       0.3       14
14                   Shrimp          oz      10.0       12
15                Snap Peas         cup       1.5  Produce
16                Soy Sauce        tbsp       2.0       22
busg9geu

busg9geu2#

在Python中可以这样做

shopping_list = friedRiceShrimp + macNCheese
final_list = []

for ingredient_to_add in shopping_list:
    # returns the first dictionary that satisfies the condition for Ingredient name and size
    # returns None if there is no dictionary in final_list that satisfies the condition
    match = next((ingredient for ingredient in final_list if
                  ingredient['Ingredient'] == ingredient_to_add['Ingredient'] and ingredient['Size'] ==
                  ingredient_to_add['Size']), None)

    # if the ingredient already exists in final_list we add to the quantity of the existing element that of the new ingredient
    if match:
        match['Quantity'] += ingredient_to_add['Quantity']

    # otherwise we simply add the ingredient
    else:
        final_list.append(ingredient_to_add)
        

for ingredient in final_list:
    print(ingredient)

产出

{'Ingredient': 'Garlic clove', 'Size': 'clove', 'Quantity': 5, 'Aisle': 'Produce'}
{'Ingredient': 'Instant Brown Rice', 'Size': 'oz', 'Quantity': 16, 'Aisle': 22}
{'Ingredient': 'Soy Sauce', 'Size': 'tbsp', 'Quantity': 2, 'Aisle': 22}
{'Ingredient': 'Chili Paste', 'Size': 'tbsp', 'Quantity': 2, 'Aisle': 22}
{'Ingredient': 'Honey', 'Size': 'tbsp', 'Quantity': 1, 'Aisle': 18}
{'Ingredient': 'Peanut Oil', 'Size': 'tbsp', 'Quantity': 2, 'Aisle': 21}
{'Ingredient': 'Shrimp', 'Size': 'oz', 'Quantity': 10, 'Aisle': 12}
{'Ingredient': 'Egg', 'Size': 'individual', 'Quantity': 3, 'Aisle': 'Dairy'}
{'Ingredient': 'Snap Peas', 'Size': 'cup', 'Quantity': 1.5, 'Aisle': 'Produce'}
{'Ingredient': 'Peanuts Dry-Roasted', 'Size': 'cup', 'Quantity': 0.3, 'Aisle': 14}
{'Ingredient': 'Bacon', 'Size': 'Slice', 'Quantity': 6, 'Aisle': 8}
{'Ingredient': 'Chicken Stock', 'Size': 'cup', 'Quantity': 2, 'Aisle': 24}
{'Ingredient': 'Milk', 'Size': 'cup', 'Quantity': 1, 'Aisle': 'Dairy'}
{'Ingredient': 'Butternut Squash puree', 'Size': 'oz', 'Quantity': 10, 'Aisle': 'Produce'}
{'Ingredient': 'Macaroni Elbow Pasta', 'Size': 'oz', 'Quantity': 10, 'Aisle': 23}
{'Ingredient': 'Broccoli', 'Size': 'cup', 'Quantity': 3, 'Aisle': 'Produce'}
{'Ingredient': 'Cheddar Cheese Shredded', 'Size': 'oz', 'Quantity': 5, 'Aisle': 8}

相关问题