如何从另一个列表创建新的运行总计列表?假设我有以下问题:
sumbl4 = LaborDetail.objects.filter(businessunit='Birmingham').filter(endofweek__gte=datetime.date.today()-datetime.timedelta(days=60)).values_list('endofweek').annotate(hours__sum=Sum('hours')).order_by('endofweek')
sumbl3 = json.dumps(list(sumbl4), cls=DecimalJSONEncoder)
我的数据格式如下(sumbl3):
[["2017/04/23", 972.5], ["2017/04/30", 1076.5], ["2017/05/07", 1162.5], ["2017/05/14", 1055.5], ["2017/05/21", 981.0], ["2017/05/28", 945.5], ["2017/06/04", 912.0], ["2017/06/11", 1106.0], ["2017/06/18", 1059.0]]
因此,我的新列表如下所示:
[["2017/04/23", 972.5], ["2017/04/30", 2049],...
注意:我使用的是python2.7
这个怎么样:
结果:
另一种获得最终列表的方法:
或者以更简洁的方式,您可以创建如下示例所示的方法/函数:
虽然^{} 没有包含在python2.7中,但是您可以使用文档中提供的方法。你知道吗
然后,它的用法如下所示:
相关问题 更多 >
编程相关推荐