I've got dictionary of lists of tuples. Each tuple has timestamp as first element and values in others. Timestamps could be different for other keys:
dict = {
'p2': [(1355150022, 3.82), (1355150088, 4.24), (1355150154, 3.94), (1355150216, 3.87), (1355150287, 6.66)],
'p1': [(1355150040, 7.9), (1355150110, 8.03), (1355150172, 8.41), (1355150234, 7.77)],
'p3': [(1355150021, 1.82), (1355150082, 2.24), (1355150153, 3.33), (1355150217, 7.77), (1355150286, 6.66)],
}
I want this convert to list of lists:
ret = [
['time', 'p1', 'p2', 'p3'],
[1355149980, '', 3.82, 1.82],
[1355150040, 7.9, 4.24, 2.24],
[1355150100, 8.03, 3.94, 3.33],
[1355150160, 8.41, 3.87, 7.77],
[1355150220, 7.77, '', ''],
[1355150280, '', 6.66, 6.66]
]
I'm making conversion with that dirty definition:
def convert_parameters_dict(dict):
tmp = {}
for p in dict.keys():
for l in dict[p]:
t = l[0] - l[0] % 60
try:
tmp[t][p] = l[1]
except KeyError:
tmp[t] = {}
tmp[t][p] = l[1]
ret = []
ret.append([ "time" ] + sorted(dict.keys()))
for t, d in sorted(tmp.iteritems()):
l = []
for p in sorted(dict.keys()):
try:
l.append(d[p])
except KeyError:
l.append('')
ret.append([t] + l)
return ret
Maybe there is some much prettier way?