Reputation: 127
I would like to merge two or more objects in the list with the same moment
enum value and add the value
to ourselves. Will be better if I write an example:
Input:
[Credit(value=111, moment=<CreditMoment.APP: 'APP'>), Credit(fee_value=222, moment=<CreditMoment.APP: 'APP'>), Credit(value=444, moment=<CreditMoment.OFFER: 'OFFER'>)]
Expected result:
[Credit(value=333, moment=<CreditMoment.APP: 'APP'>), Credit(value=444, moment=<CreditMoment.OFFER: 'OFFER'>)]
As you can see now the expected list has 2 elements which first element has combined value=333
The elements came from this class:
class CreditMoment(str, AutoNameEnum):
APP= auto()
OFFER = auto()
COMPLETION = auto()
Upvotes: 0
Views: 543
Reputation: 452
Something like this will help.
dic = {}
for credit in lis:
# Will create a dict that willh have moment value as key and sum all the values
dic[credit.moment.value] = dic.get(credit.moment.value, 0) + credit.value
# Now recreate the list
[Credit(value=v, moment=x) for x,v in dic.items()]
Upvotes: 1