ruby task

 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
from itertools import groupby
log = [
{"time": 201201, "x": 2},
{"time": 201201, "y": 7},
{"time": 201201, "z": 2},
{"time": 201202, "a": 3},
{"time": 201202, "b": 4},
{"time": 201202, "c": 0}
]
def transmogify(data):
res = []
for k, g in groupby(data, lambda d: d['time']):
d = {'time': k}
for dic in g:
d.update(dic)
res.append(d)
return res
print(transmogify(log))
# [{'y': 7, 'x': 2, 'z': 2, 'time': 201201}, {'a': 3, 'c': 0, 'b': 4, 'time': 201202}]