我从SQL查询得到了以下未规范化的元组列表。
data = [(...) {u'timestamp': datetime.datetime(2016, 5, 25, 23, 55, 33), u'sensor': u'GP_PRES', u'value': u'96587'}, {u'timestamp': datetime.datetime(2016, 5, 25, 23, 56, 33), u'sensor': u'GP_NO2', u'value': u'0.221'}, {u'timestamp': datetime.datetime(2016, 5, 25, 23, 56, 33), u'sensor': u'GP_TC', u'value': u'14.600'}, {u'timestamp': datetime.datetime(2016, 5, 25, 23, 56, 33), u'sensor': u'GP_HUM', u'value': u'83.94'}, (...) ]
这些是来自多个传感器的传感器数据,但具有相同的时间戳。
我想将所有传感器值分组为相同的时间戳。所以我有一个每个相等时间戳的元组列表。
grouped_data = [... {u'timestamp': datetime.datetime(2016, 5, 25, 23, 55, 33), [{u'sensor': u'GP_PRES', u'value': u'96587'}, { u'sensor': u'GP_NO2', u'value': u'0.221'}, {u'sensor': u'GP_TC', u'value': u'14.600'}, {u'sensor': u'GP_HUM', u'value': u'83.94'}]}, ... ]
我知道我可以使用itertools中的groupby()函数。我不明白我需要什么样的Key函数才能按相等的'timestamp'进行分组。
data2 = sorted(data, key= operator.attrgetter('timestamp'))
for k, g in groupby(data2, keyfunc):
groups.append(list(g))
答案 0 :(得分:1)
在您的代码中,operator.attrgetter('timestamp')
会引发错误AttributeError: 'dict' object has no attribute 'timestamp'
。
将其替换为key=lambda d:d['timestamp']
,如下所示。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import itertools
import operator
import datetime
data = [{u'timestamp': datetime.datetime(2016, 5, 25, 23, 55, 33), u'sensor': u'GP_PRES', u'value': u'96587'},
{u'timestamp': datetime.datetime(2016, 5, 25, 23, 56, 33), u'sensor': u'GP_NO2', u'value': u'0.221'},
{u'timestamp': datetime.datetime(2016, 5, 25, 23, 56, 33), u'sensor': u'GP_TC', u'value': u'14.600'},
{u'timestamp': datetime.datetime(2016, 5, 25, 23, 56, 33), u'sensor': u'GP_HUM', u'value': u'83.94'}]
groups = list()
data2 = sorted(data, key=lambda d:d['timestamp'])
for k, g in itertools.groupby(data2, key=lambda d:d['timestamp']):
groups.append(list(g))
print(groups) # 2 groups
[[ {u'timestamp': datetime.datetime(2016, 5, 25, 23, 55, 33), u'sensor': u'GP_PRES', u'value': u'96587'}],
[ {u'timestamp': datetime.datetime(2016, 5, 25, 23, 56, 33), u'sensor': u'GP_NO2', u'value': u'0.221'},
{u'timestamp': datetime.datetime(2016, 5, 25, 23, 56, 33), u'sensor': u'GP_TC', u'value': u'14.600'},
{u'timestamp': datetime.datetime(2016, 5, 25, 23, 56, 33), u'sensor': u'GP_HUM', u'value': u'83.94'}]]