我想建立一个简单的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,并试图找到在日常生活中使用它的方法,以帮助巩固想法和概念。
2条答案
按热度按时间hjqgdpho1#
实际上,您正在尝试合并/累计具有相同
('Ingredient', 'Size')
属性的重复记录,并对它们的Quantity
求和。使用
pandas
幂对所需列进行group by运算,并按需要计算或约简的其他列进行聚合。busg9geu2#
在Python中可以这样做
产出