Running total for list of dict

2024/11/8 15:35:37

Have a python list of dict as following:

Dict1 = [{'date': 1, 'name': 'xyz', 'qty': 100},{'date': 1, 'name': 'xyz', 'qty': 200},{'date': 1, 'name': 'xyz', 'qty': 300},{'date': 1, 'name': 'xyz2', 'qty': 30},{'date': 2, 'name': 'xyz', 'qty': 1000},{'date': 2, 'name': 'xyz2', 'qty': 300},{'date': 3, 'name': 'xyz', 'qty': 500},{'date': 3, 'name': 'xyz2', 'qty': 500},{'date': 3, 'name': 'xyz3', 'qty': 500},{'date': 3, 'name': 'xyz', 'qty': 600}]

Would like to to get running total of qty for each name for each date:

date:1,name:xyz,qty:600
date:1,name:xyz2,qty:30
date:2,name:xyz,qty:1600
date:2,name:xyz2,qty:330
date:3,name:xyz,qty:2700
date:3,name:xyz2,qty:830
date:3,name:xyz3,qty:500

Thanks.

Answer
from itertools import groupby
from operator import itemgetter
for k, gr in groupby(Dict1, key=itemgetter('date', 'name')):print "date:%i,name:%s,qty:%i" % (k[0], k[1], sum(d['qty'] for d in gr))
https://en.xdnf.cn/q/120605.html

Related Q&A

How to convert CSV file to a specific JSON format with nested objects?

I want to populate my json message with data from a CSV file. I want each row to be a "new" json object. I am using Python and will connect the the code to an API once done. Some of the data …

How do I repeat the program? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

How to parse json data in Python? [duplicate]

This question already has answers here:How can I parse (read) and use JSON in Python?(5 answers)Closed 10 years ago.Please help me to parse this json in python.{ "IT" : [ { "firstName…

Why did they opt for the message most recent call last? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed last month.Improve …

AttributeError: NoneType object has no attribute current

class LoginScreen(Screen):def __init__(self,**kwargs):super(LoginScreen, self).__init__(**kwargs)print self,self.parent.currentclass AppScreenManager(ScreenManager):pass#Base Class class AppBaseClass(A…

Python urllib2 or requests post method [duplicate]

This question already has answers here:Submitting to a web form using python(3 answers)Closed 8 years ago.The community reviewed whether to reopen this question 9 months ago and left it closed:Original…

How to fix Django NoReverseMatch Error

I have built a simple blog app with Django and I have had some issue with NoReverseMatch. Here are my problem screenshots: https://prnt.sc/imqx70 https://prnt.sc/imqwptHere is my code on Github: https:…

Selenium NoSuchElementException with driver.find_element_by_xpath

First question: How do I make python minimize chrome? Second question: When getting to the end page using the next button how do I tell python to go on.. and not give me an error?driver.get("htt…

Traverse Non-Binary Tree [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Muscle alignment in python

I have a problem with printing my output from muscle aligning in python. My code is:from Bio.Align.Applications import MuscleCommandline from StringIO import StringIO from Bio import AlignIOdef align_v…