python - Python中的累积积
问题描述
我正在尝试编写一个算法来给我这样的累积产品A=[1, 3, 5, 6, 8]
,cumprodA = [1, 3, 15, 90, 720]
. 我认为对于经验丰富的统计学家来说这并不难,但我的编程技能有点薄弱。
解决方案
Start with your multiplicative identity (1
), and multiply it by each number, remembering the result and appending to a cumprod list while you do so.
def cumprod(lst):
results = []
cur = 1
for n in lst:
cur *= n
results.append(cur)
return results
Note that this is the general solution for any Monoidal operation. Start with the mzero
and do the same steps. Cumulative sum starts with 0
, Cumulative list extends starts with []
, etc.
推荐阅读
- swiftui - 在 SwiftUI 中使用带有 ScrollView 的 LazyVStack 和 LazyHStack 的屏幕/滚动性能不佳
- powershell - 最佳实践:只是为了参数完成而添加参数?
- java - 我的 android 应用程序在所有其他手机上崩溃,而不是在开发期间用于测试
- web - Certify origin of downloaded file
- go - grpc/go 如何在 grpc.Dial 中设置 grpc.ssl_target_name_override
- apache-flink - FlinkKinesisProducer 中的订购保证
- android - 使用 Glide 从 URL 加载图像时出现 IllegalArgumentException?
- css - 使用 css 进行转换
- javascript - 有没有一种方法可以通过 2 个字段的总和对猫鼬模型进行排序?
- html - 绝对位置的页面填充高度