问题描述
产生累积金额的好方法是什么?
例如,给定列表x,
cumx = x [:]
for i in range(1,len(x)):
cumx [i] = cumx [i] + cumx [i-1]
什么是更好的方式?
谢谢,
Alan Isaac
What''s the good way to produce a cumulative sum?
E.g., given the list x,
cumx = x[:]
for i in range(1,len(x)):
cumx[i] = cumx[i]+cumx[i-1]
What''s the better way?
Thanks,
Alan Isaac
推荐答案
有什么不做的事,或者是什么
它确实这样做不应该吗?
你可以这样做:
#未经测试
def cumulative_sum(L):
CL = []
csum = 0
for x in L:
csum + = x
CL.append(csum)
返回CL
无论是好还是坏都取决于你对b $ b的考虑好坏。
-
史蒂文。
Is there something that this doesn''t do, or something
it does do that it shouldn''t?
You could do it this way:
# untested
def cumulative_sum(L):
CL = []
csum = 0
for x in L:
csum += x
CL.append(csum)
return CL
Whether it is better or worse depends on what you
consider better or worse.
--
Steven.
有什么不做的事吗,或者什么
它确实这样做它不应该吗?
你可以这样做:
#未经测试
def cumulative_sum(L):
CL = []
csum = 0
for x in L:
csum + = x
CL.append(csum)
返回CL
无论是好还是坏,都取决于你的价值是多少还是更糟。
-
史蒂文。
Is there something that this doesn''t do, or something
it does do that it shouldn''t?
You could do it this way:
# untested
def cumulative_sum(L):
CL = []
csum = 0
for x in L:
csum += x
CL.append(csum)
return CL
Whether it is better or worse depends on what you
consider better or worse.
--
Steven.
定义更好。更准确的?更少的代码?
-
Robert Kern
在草地长得高的地狱里
是否允许死亡的坟墓死亡。
- Richard Harter
Define better. More accurate? Less code?
--
Robert Kern
ro*********@gmail.com
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
这篇关于最佳累计金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!