usr bin env python coding utf-8 from __future__ import division print_

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
# coding: utf-8
from __future__ import division, print_function, unicode_literals
from itertools import groupby
from future_builtins import *
dicts=[
{'date': '2014-06-30', "obj_id": 1, 'value': 4, 'total': True},
{'date': '2014-07-01', "obj_id": 1, 'value': 1},
{'date': '2014-07-02', "obj_id": 1, 'value': 3},
{'date': '2014-06-30', "obj_id": 2, 'value': 6, 'total': True},
{'date': '2014-07-01', "obj_id": 2, 'value': 4},
{'date': '2014-07-02', "obj_id": 2, 'value': 2},
{'date': '2014-06-30', "obj_id": 3, 'value': 7, 'total': True},
{'date': '2014-07-01', "obj_id": 3, 'value': 5},
{'date': '2014-07-02', "obj_id": 3, 'value': 2},
]
res = []
for key,group in groupby(dicts, lambda x: x['obj_id']):
tmp = {}
data = []
tmp['obj_id'] = key['obj_id']
tmp['total'] = 0
for x in group:
tmp['total'] += x['value']
total = x.get('total')
if total:
tmp['total'] = total
data.append(x)
# here sort data by datatime
# надеюсь дату можно так сравнимать?
data.sort(key=lambda x: x['data'] )
tmp['data'] = data
res.append(tmp)