我有一个redis数据库,它每十秒钟从Arduino接收一次数据。
现在,我想制作六个十秒钟的数据来计算一个六十二秒的数据,然后获得六个十秒钟数据的平均值,最大值,最小值,如下所示。
import json
a = [u'{"id":"proximity_sensor1","tstamp":1440643570238,"avg":15.0,"coefVariation":0.0,"anom":0,"max":15.0,"min":15.0,"sample_size":10}',
u'{"id":"proximity_sensor1","tstamp":1440643580307,"avg":15.0,"coefVariation":0.0,"anom":0,"max":15.0,"min":15.0,"sample_size":9}',
u'{"id":"proximity_sensor1","tstamp":1440643590242,"avg":15.0,"coefVariation":0.0,"anom":0,"max":15.0,"min":15.0,"sample_size":9}',
u'{"id":"proximity_sensor1","tstamp":1440643590242,"avg":15.0,"coefVariation":0.0,"anom":0,"max":15.0,"min":15.0,"sample_size":8}',
u'{"id":"proximity_sensor1","tstamp":1440643590242,"avg":15.0,"coefVariation":0.0,"anom":0,"max":15.0,"min":15.0,"sample_size":9}',
u'{"id":"proximity_sensor1","tstamp":1440643590242,"avg":15.0,"coefVariation":0.0,"anom":0,"max":15.0,"min":15.0,"sample_size":9}']
a = map(lambda x: json.loads(x), a)
#print a
def abc(aaa):
for index in range(0, len(aaa), 6):
abc = aaa[index:(index+6)]
tuples = [('avg', 'max', 'min')]
avg = sum(map(lambda x: x['avg'], abc))/6
min_ = min(map(lambda x: x['min'], abc))
max_ = max(map(lambda x: x['max'], abc))
yield [avg, max_, min_]
print list(abc(a))
我在想是否有更好的解决方法。如果我使用itertools.groupby
,我可以更快地解决它吗?或者任何人都有好主意简化计算过程?
答案 0 :(得分:1)
正常情况下,itertools.groupby与某些条件一起使用来对元素进行分组,但是因为在你的情况下你没有任何这样的条件,相反你只想将每6个元素组合在一起,我不认为使用itertools .groupby会带来任何好处。
话虽如此,我可以提出一些其他改进建议 -
您可以使用'键' max / min函数的参数而不是map / lambda的当前方法,例如 -
max_ = max(abc, key= lambda x:x['max'])['max']
同样适用于min()函数。
另外,我认为对sum()而不是map / lambda进行列表理解会更具可读性。示例 -
avg = sum([x['avg'] for x in abc])/6