简体   繁体   中英

Python: How to merge AND sum list values based on some criteria

I'm using python 3.6. I've some result from an API call like this:

[
  { "order_id": 51128352, "item_id": 17811608, "amount": -1.74 },
  { "order_id": 51128352, "item_id": 17811608, "amount": 13.88 },
  { "order_id": 50290147, "item_id": 17811608, "amount": -1.74 },
  { "order_id": 50290147, "item_id": 17811608, "amount": 20.34 },
  { "order_id": 50320149, "item_id": 13397933, "amount": -5.78 },
  { "order_id": 50320149, "item_id": 13397933, "amount": 23.12 }
]

Now, my first goal is to merge the list values AND sum the "amount" value WHERE "order_id" is same. So, the results should be something like this:

[
  { "order_id": 51128352, "item_id": 17811608, "amount": 12.14 },
  { "order_id": 50290147, "item_id": 17811608, "amount": 18.6 },
  { "order_id": 50320149, "item_id": 13397933, "amount": 17.34 }
]

Once, I get this result now I want to merge this new list values AND sum the "amount" value WHERE "item_id" is same. PLUS I also want to remove "order_id" from the result, since then it would be irrelevant. So, the results should be something like this:

[
  { "item_id": 17811608, "amount": 30.74 },
  { "item_id": 13397933, "amount": 17.34 }
]

How should I do it?

First, define get_order_id as a function used to sort your input and as a key to itertools.groupby (input could be unsorted order_id-wise and groupby wouldn't work properly in that case)

Once grouped, just rebuild a list of simplified dicts, with order_id and amount as keys, and the sum of amounts for amount value.

l = [
  { "order_id": 51128352, "item_id": 17811608, "amount": -1.74 },
  { "order_id": 50290147, "item_id": 17811608, "amount": -1.74 },
  { "order_id": 50290147, "item_id": 17811608, "amount": 20.34 },
  { "order_id": 50320149, "item_id": 13397933, "amount": -5.78 },
  { "order_id": 51128352, "item_id": 17811608, "amount": 13.88 },
  { "order_id": 50320149, "item_id": 13397933, "amount": 23.12 }
]

import itertools

get_order_id = lambda x : x["order_id"]

result = [{'amount':  sum(x['amount'] for x in r), 'item_id' : k} 
          for k,r in itertools.groupby(sorted(l,key=get_order_id),key=get_order_id)]

print(result)

result:

[{'amount': 18.6, 'item_id': 50290147}, {'amount': 17.34, 'item_id': 50320149}, {'amount': 12.14, 'item_id': 51128352}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM