Home > front end >  Order list by key and build sum over list items - python
Order list by key and build sum over list items - python

Time:06-10

I want to create a shopping list based on meal selections in an ecxel file. What I have is a list list_flat_ingredients imported from a csv with this format: [ingredient_amount, ingredient_unit, ingredient_name]

Example content:

[
    [1.0, "0", "Obst nach Wahl für Frühstücksbowl"],
    [1.0, "Stück", "Schalotten"],
    [1.0, "Stück", "Süßkartoffeln"],
    [1.0, "0", "Obst nach Wahl für Frühstücksbowl"],
    [500.0, "g", "Spätzle"],
    [100.0, "g", "Tunfisch (Dose)"],
    [1.0, "Laib", "Brot"],
    [250.0, "g", "Spaghetti"],
    [1.0, "0", "Spargel, grün"],
    [1.0, "Laib", "Brot"],
    [4.0, "Stück", "Burger Buns"],
    [1.0, "Stück", "Flammkuchenteig"],
    [1.0, "Laib", "Brot"],
    [1.0, "g", "Berglinsen"],
    [4.0, "Stück", "Wiener"],
    [1.0, "0", "Obst nach Wahl für Frühstücksbowl"],
    [1.0, "g", "Berglinsen"],
    [1.0, "Stück", "Baguette"],
    [1.0, "Laib", "Brot"],
    [150.0, "g", "Garnelen"],
    [1.0, "0", "Hirtenkäse "],
    [1.0, "Becher", "Joghurt"],
    [1.0, "Stück", "Fenchel"],
    [1.0, "Kopf", "Salat"],
    [1.0, "Becher", "Joghurt"],
    [200.0, "g", "Bergkäse"],
    [0.25, "Kopf", "Salat"],
    [1.0, "0", "Aufstrich nach Wahl für Frühstück"],
    [1.0, "0", "Eier"],
    [1.0, "0", "Zitrone"],
    [1.0, "0", "Aufstrich nach Wahl für Frühstück"],
    [1.0, "0", "Ziegencamembert "],
    [1.0, "0", "Hummus"],
    [1.0, "0", "Aufstrich nach Wahl für Frühstück"],
    [1.0, "0", "Paprika"],
    [1.0, "0", "Brezen"],
    [1.0, "Becher", "Joghurt"],
    [1.0, "0", "Paprika"],
    [100.0, "g", "Speck"],
    [1.0, "0", "Aufstrich nach Wahl für Frühstück"],
    [1.0, "0", "Reis nach Wahl"],
    [1.0, "0", "Paprika"],
    [1.0, "0", "Chiasamen"],
    [1.0, "0", "Lachs"],
    [1.0, "Stück", "Gurke"],
    [1.0, "0", "Chiasamen"],
    [2.0, "Stück", "Zwiebeln"],
    [2.0, "Stück", "Tomaten"],
    [0.0, "0", "0"],
    [1.0, "0", "Parmesan"],
    [1.0, "0", "Weißwein"],
    [0.0, "0", "0"],
    [1.0, "0", "Tomaten"],
    [1.0, "0", "Zucchini"],
    [0.0, "0", "0"],
    [1.0, "0", "Gurke"],
    [...]
]

What I then do is group and sum the elements with

for i, g in groupby(sorted(list_flat_ingredients), key=lambda x: x[2]):
    shopping_list_in_order.append([sum(v[0] for v in g), i])

The problem is that I loose the element ingredient_unit in the final output (example):

[
    [0.0, "0"],
    [0.25, "Salat"],
    [0.5, "Gurke"],
    [1.0, "Artischocken"],
    [4.0, "Aufstrich nach Wahl für Frühstück"],
    [1.0, "Brezen"],
    [1.0, "Burger Sauce"],
    [...],
]

Where and how do I need to add this in the code? THANKS!

CodePudding user response:

You'll need to do the the summing manually, as the group variables generated by groupby are iterables, so they can only be used once. See both this answer and this answer for more details.

shopping_list_in_order = []
for _, group in groupby(sorted(list_flat_ingredients), key=lambda x: x[2]):
    row = []
    for elem in group:
        if len(row) == 0:
            row = elem
        else:
            row[0]  = elem[0]
    shopping_list_in_order.append(row)

from pprint import pprint
pprint(shopping_list_in_order)

output

[[0.0, '0', '0'],
 [0.25, 'Kopf', 'Salat'],
 [4.0, '0', 'Aufstrich nach Wahl für Frühstück'],
 [1.0, '0', 'Brezen'],
 [2.0, '0', 'Chiasamen'],
 [1.0, '0', 'Eier'],
 [1.0, '0', 'Gurke'],
 [1.0, '0', 'Hirtenkäse '],
 [1.0, '0', 'Hummus'],
 [1.0, '0', 'Lachs'],
 [3.0, '0', 'Obst nach Wahl für Frühstücksbowl'],
 [3.0, '0', 'Paprika'],
 [1.0, '0', 'Parmesan'],
 [1.0, '0', 'Reis nach Wahl'],
 [1.0, '0', 'Spargel, grün'],
 [1.0, '0', 'Tomaten'],
 [1.0, '0', 'Weißwein'],
 [1.0, '0', 'Ziegencamembert '],
 [1.0, '0', 'Zitrone'],
 [1.0, '0', 'Zucchini'],
 [3.0, 'Becher', 'Joghurt'],
 [1.0, 'Kopf', 'Salat'],
 [4.0, 'Laib', 'Brot'],
 [1.0, 'Stück', 'Baguette'],
 [1.0, 'Stück', 'Fenchel'],
 [1.0, 'Stück', 'Flammkuchenteig'],
 [1.0, 'Stück', 'Gurke'],
 [1.0, 'Stück', 'Schalotten'],
 [1.0, 'Stück', 'Süßkartoffeln'],
 [2.0, 'g', 'Berglinsen'],
 [2.0, 'Stück', 'Tomaten'],
 [2.0, 'Stück', 'Zwiebeln'],
 [4.0, 'Stück', 'Burger Buns'],
 [4.0, 'Stück', 'Wiener'],
 [100.0, 'g', 'Speck'],
 [100.0, 'g', 'Tunfisch (Dose)'],
 [150.0, 'g', 'Garnelen'],
 [200.0, 'g', 'Bergkäse'],
 [250.0, 'g', 'Spaghetti'],
 [500.0, 'g', 'Spätzle']]

CodePudding user response:

My solution is based on dict using.

list_flat_ingredients = [
    [1.0, "0", "Obst nach Wahl für Frühstücksbowl"],
    [1.0, "Stück", "Schalotten"],
    [1.0, "Stück", "Süßkartoffeln"],
    [1.0, "0", "Obst nach Wahl für Frühstücksbowl"],
    [500.0, "g", "Spätzle"],
    [100.0, "g", "Tunfisch (Dose)"],
    [1.0, "Laib", "Brot"],
    [250.0, "g", "Spaghetti"],
    [1.0, "0", "Spargel, grün"],
    [1.0, "Laib", "Brot"],
    [4.0, "Stück", "Burger Buns"],
    [1.0, "Stück", "Flammkuchenteig"],
    [1.0, "Laib", "Brot"],
    [1.0, "g", "Berglinsen"],
    [4.0, "Stück", "Wiener"],
    [1.0, "0", "Obst nach Wahl für Frühstücksbowl"],
    [1.0, "g", "Berglinsen"],
    [1.0, "Stück", "Baguette"],
    [1.0, "Laib", "Brot"],
    [150.0, "g", "Garnelen"],
    [1.0, "0", "Hirtenkäse "],
    [1.0, "Becher", "Joghurt"],
    [1.0, "Stück", "Fenchel"],
    [1.0, "Kopf", "Salat"],
    [1.0, "Becher", "Joghurt"],
    [200.0, "g", "Bergkäse"],
    [0.25, "Kopf", "Salat"],
    [1.0, "0", "Aufstrich nach Wahl für Frühstück"],
    [1.0, "0", "Eier"],
    [1.0, "0", "Zitrone"],
    [1.0, "0", "Aufstrich nach Wahl für Frühstück"],
    [1.0, "0", "Ziegencamembert "],
    [1.0, "0", "Hummus"],
    [1.0, "0", "Aufstrich nach Wahl für Frühstück"],
    [1.0, "0", "Paprika"],
    [1.0, "0", "Brezen"],
    [1.0, "Becher", "Joghurt"],
    [1.0, "0", "Paprika"],
    [100.0, "g", "Speck"],
    [1.0, "0", "Aufstrich nach Wahl für Frühstück"],
    [1.0, "0", "Reis nach Wahl"],
    [1.0, "0", "Paprika"],
    [1.0, "0", "Chiasamen"],
    [1.0, "0", "Lachs"],
    [1.0, "Stück", "Gurke"],
    [1.0, "0", "Chiasamen"],
    [2.0, "Stück", "Zwiebeln"],
    [2.0, "Stück", "Tomaten"],
    [0.0, "0", "0"],
    [1.0, "0", "Parmesan"],
    [1.0, "0", "Weißwein"],
    [0.0, "0", "0"],
    [1.0, "0", "Tomaten"],
    [1.0, "0", "Zucchini"],
    [0.0, "0", "0"],
    [1.0, "0", "Gurke"],
]

result_dict = {}
result_list = []

for item in list_flat_ingredients:
    if item[2] not in result_dict:
        result_dict[item[2]] = [item[0], item[1]]
    else:
        result_dict[item[2]][0]  = item[0]

# if you need result as list
for item in result_dict.keys():
    result_dict[item].append(item)
    result_list.append(result_dict[item])
  • Related