【发布时间】:2018-01-04 13:36:19
【问题描述】:
我在查找类似于累积总和的函数时遇到问题,只是我希望它加权。因此,使用外部 for 循环看起来像这样:
discount_rate = 0.95
rewards = [0, 0, 10] # want to translate to: [9, 9.5, 10] (approximation)
reversed_rewards = [10, 0, 0]
new_rewards = [0] * len( rewards)
previus = 0
for index in range( len( rewards)):
new_rewards[ index] = reversed_rewards[ index] + previus * discount_rate
previus = new_rewards[ index]
print( list( reversed( new_rewards)))
但是如果你有大量的奖励数组,这是一种缓慢的版本。是否有任何现有的功能可以更快地做到这一点?
【问题讨论】:
-
它看起来确实有点像骗子,但尽管有
numpy标签,这个问题实际上并没有像目前写的那样使用numpy。