<ol>
<li><p>避免使用十进制-你在这里所做的一切都不需要绝对的精度。</p></li>
<li><p>转换if..然后使用<code>bisect</code></p>级联为二进制查找</li>
<li><p>我不太明白你的数学到底能完成什么,但我对你在这里使用<code>abs()</code>的用法持怀疑态度。你确定这就是你想要的吗?</p></li>
<li><p>这可能不是一个好主意,但是为了演示:我使用<code>yield</code>语句将函数转换为生成器函数;它不是返回字符串数组,而是一次返回一个字符串。</p></li>
</ol>
<p>一。在</p>
<pre><code>from bisect import bisect_left
breakpoints = [-1., 2., 3., 4., 6., 10., 20., 30., 50., 100.]
ticks = [0., 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1., 2., 5., 10.]
def offset_back(initial_bet, initial_odds, breakpoints=breakpoints, ticks=ticks):
new_odds = initial_odds
while new_odds > 1.:
# calculate tick based on size of odds
tick = ticks[bisect_left(breakpoints, new_odds)]
# calculate percentage difference between initial bet and new odds
drop = 1. - initial_odds / new_odds
# calculate new stake, increased to cancel liability
new_bet = initial_bet + abs(initial_bet * drop) # are you sure abs() is correct here?
# calculate potential profit based on the new stake - initial invested stake
profit = new_bet - initial_bet
new_odds -= tick
yield " Stake: {stake:0.2f} Odds: {odds:0.2f} Profit: {profit:0.2f}".format(stake=abs(new_bet), odds=abs(new_odds), profit=abs(profit))
</code></pre>