Python 程序查找列表的累计和
pythonserver side programmingprogramming
在本文中,我们将了解下面给出的问题陈述的解决方案。
问题陈述− 我们有一个列表,我们需要创建一个包含累计和的列表。
现在让我们观察下面实现中的解决方案 −
示例
# 累计和 def Cumulative(l): new = [] cumsum = 0 for element in l: cumsum += element new.append(cumsum) return new # 驱动代码 lists = [10, 20, 30, 40, 50] print ("New list:",Cumulative(lists))
输出
New list: [10, 30, 60, 100, 150]
所有变量均在本地范围内声明,其引用如上图所示。
结论
在本文中,我们了解了如何编写 Python 程序来查找累计和列表。