Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best pythonic way to populate the list containing the date type data?

Tags:

python

list

I have the following list data.

data = [['2009-01-20', 3000.0], ['2011-03-01', 6000.0], ['2008-12-15',
6000.0], ['2002-02-15', 6000.0], ['2009-04-20', 6000.0], ['2010-08-01',
4170.0], ['2002-07-15', 6000.0], ['2008-08-15', 6000.0], ['2010-12-01',
6000.0], ['2011-02-01', 8107.0], ['2011-04-01', 8400.0], ['2011-05-15',
9000.0], ['2010-05-01', 6960.0], ['2005-12-15', 6000.0], ['2010-10-01',
6263.0], ['2011-06-02', 3000.0], ['2010-11-01', 4170.0], ['2009-09-25',
6000.0]]

where the first argument is date & second argument is total. i want result using group by month & year from the above list.

i.e result would like:

--> for month: [['JAN',tot1],['FEB',tot2],['MAR',tot3] ...]
--> for year: [['2002',tot1],['2005',tot2],['2008',tot3] ...]
like image 232
Avadhesh Avatar asked Jun 10 '11 05:06

Avadhesh


2 Answers

from collections import defaultdict

yeartotal = defaultdict(float)
monthtotal = defaultdict(float)
for s in data:
    d = s[0].split('-')
    yeartotal[d[0]] += s[1]
    monthtotal[d[1]] += s[1]


In [37]: [item for item in yeartotal.iteritems()]
Out[37]: 
[('2002', 12000.0),
 ('2005', 6000.0),
 ('2008', 12000.0),
 ('2009', 15000.0),
 ('2011', 34507.0),
 ('2010', 27563.0)]

In [38]: [item for item in monthtotal.iteritems()]
Out[38]: 
[('02', 14107.0),
 ('03', 6000.0),
 ('12', 18000.0),
 ('06', 3000.0),
 ('07', 6000.0),
 ('04', 14400.0),
 ('05', 15960.0),
 ('08', 10170.0),
 ('09', 6000.0),
 ('01', 3000.0),
 ('11', 4170.0),
 ('10', 6263.0)]
like image 180
Steve Tjoa Avatar answered Oct 07 '22 23:10

Steve Tjoa


First, lets transform the data into a more convenient form. We'll use the datetime module to handle those dates.

>>> trans = lambda row: (datetime.datetime.strptime(row[0], "%Y-%m-%d"), row[1])
>>> tdata = map(trans, data)

Next, a function (one each for the two group operations) that sums the value into a dict with the corresponding group.

>>> def mker(left, right):
...     result = dict(left)
...     mo = right[0].strftime('%b')
...     result[mo] = right[1] + left.get(mo, 0)
...     return result
... 
>>> def yker(left, right):
...     result = dict(left)
...     mo = right[0].strftime('%Y')
...     result[mo] = right[1] + left.get(mo, 0)
...     return result
... 

Finally, we apply these kernel functions to the data using reduce()

>>> reduce(mker, tdata, {})
{'Apr': 14400.0,
 'Aug': 10170.0,
 'Dec': 18000.0,
 'Feb': 14107.0,
 'Jan': 3000.0,
 'Jul': 6000.0,
 'Jun': 3000.0,
 'Mar': 6000.0,
 'May': 15960.0,
 'Nov': 4170.0,
 'Oct': 6263.0,
 'Sep': 6000.0}
>>> reduce(yker, tdata, {})
{'2002': 12000.0,
 '2005': 6000.0,
 '2008': 12000.0,
 '2009': 15000.0,
 '2010': 27563.0,
 '2011': 34507.0}
like image 39
SingleNegationElimination Avatar answered Oct 07 '22 22:10

SingleNegationElimination