如何从其他列表创建新的运行总计列表?假设我有以下问题:
true
true
true
true
false
false
false
我的数据采用这种格式(sumbl3):
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)
所以我的新列表看起来像是:
[["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]]
注意:我使用的是Python 2.7
答案 0 :(得分:0)
这个怎么样:
orig = [["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]]
new = []
temp_sum = 0
for o in orig:
a = o[0]
b = o[1] + temp_sum
new.append([a, b])
temp_sum += b
print new
结果:
[['2017/04/23', 972.5], ['2017/04/30', 2049.0], ['2017/05/07', 4184.0], ['2017/05/14', 8261.0], ['2017/05/21', 16447.5], ['2017/05/28', 32859.5], ['2017/06/04', 65685.5], ['2017/06/11', 131565.0], ['2017/06/18', 263083.0]]
答案 1 :(得分:0)
另一种让你获得最终名单的方法:
a = [['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]]
sub = [a[0]]
for k, v in a[1:]:
b = sub[-1][1] + v
sub.append([k, b])
print(sub)
>>> [['2017/04/23', 972.5],
['2017/04/30', 2049.0],
['2017/05/07', 3211.5],
['2017/05/14', 4267.0],
['2017/05/21', 5248.0],
['2017/05/28', 6193.5],
['2017/06/04', 7105.5],
['2017/06/11', 8211.5],
['2017/06/18', 9270.5]]
或者以更紧凑的方式,您可以创建一个方法/函数,如下例所示:
def get_sum(a):
sub = []
for k, v in a:
if not sub:
sub.append([k, v])
else:
sub.append([k, sub[-1][1] +v])
return sub
答案 2 :(得分:0)
虽然Python 2.7中不包含itertools.accumulate
,但您可以使用文档中提供的配方。
import operator
def accumulate(iterable, func=operator.add):
'Return running totals'
# accumulate([1,2,3,4,5]) --> 1 3 6 10 15
# accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
it = iter(iterable)
try:
total = next(it)
except StopIteration:
return
yield total
for element in it:
total = func(total, element)
yield total
然后,它的用法如下:
data = [(1, 1), (2, 2), (3, 3)]
keys, values = zip(*data)
accumulated_data = zip(keys, accumulate(values))
# [(1, 1), (2, 3), (3, 6)]
答案 3 :(得分:0)
这个解决方案给了我正在寻找的东西:
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)
list_of_running_totals = []
running_total = 0
for date, amount in sumbl4:
running_total += amount
list_of_running_totals.append([date, running_total])
print list_of_running_totals