擅长:python、mysql、java
<p>你对<code>cumsum</code>和<code>diff</code>的想法奏效了。它看起来不太复杂;不确定是否有更短的解决方案。首先,我们计算累计和,然后进行运算,然后返回(<code>diff</code>有点像{<cd1>}的逆函数)。在</p>
<pre><code>import math
c = values.cumsum() - ALLOWANCE
# now we've got [-15, -5, 20, 50]
c[c < 0] = 0 # negative values don't make sense here
# (c - c.shift(1)) # <-- what I had first: diff by accident
# it is important that we don't fill with 0, in case that the first
# value is greater than ALLOWANCE
c.diff().fillna(math.max(0, values[0] - ALLOWANCE))
</code></pre>