【发布时间】:2021-09-10 17:46:19
【问题描述】:
问题:如何补全和简化下面的语法以达到想要的输出?
期望的输出:
[16.0, 22.0, 26.0, 15.0, 18.0, 21.0, 5.0, 6.0, 8.0]
当前语法:
a = [16.0, 6.0, 4.0, 15.0, 3.0, 3.0, 5.0, 1.0, 2.0] #---> there is 3 sets of data in here,
assuming the number of sets will
change at different point in time
b = 3 #----> this represents the number of dataset in "a", and will to change at different
point in time
c = []
for idx, i in enumerate(a[0:3]):
if idx == 0:
c.append(i)
else:
c.append(a[idx-1] + i)
for idx, i in enumerate(a[3:6]):
if idx == 0:
c.append(i)
else:
c.append(a[idx-1] + i)
for idx, i in enumerate(a[6:9]):
if idx == 0:
c.append(i)
else:
c.append(a[idx-1] + i)
print(c)
【问题讨论】:
-
你是在问如何计算给定
b = 3的数字0、3、6、9? -
@mkrieger1 OP 想要三个一组的累积总和。
-
@DreamyDeerz 您是否得到了任何一个已发布答案的帮助?如果是这样,请投票并将其标记为正确,以便可以从未答复队列中删除此帖子。
标签: python python-3.x for-loop