Python sum() 正在为两个不同的列表计算相同的值

Python sum() is calculating same value for two different lists

我正在尝试使用 RSI 进行技术分析,其中涉及有 2 个收益和损失列表,然后获取 2 个列表的平均值。出于某种原因,当使用 sum() 时,我收到两个列表的相同值。手动添加列表总和时,我看到了不同的结果。这是我的:

def calculate_avg_gain(in_list : list) -> tuple:
gain_list = []
loss_list = []
for i, close in enumerate(in_list):
    try:
        change = close - in_list[i-1]
    except IndexError:
        change = 0.0
    if change >= 0.0:
        gain_list.append(change)
        loss_list.append(0.0)
    else:
        gain_list.append(0.0)
        loss_list.append(abs(change))
avg_gain = sum(gain_list) / 14
avg_loss = sum(loss_list) / 14
return avg_gain, avg_loss

我在每个条目有 14 个条目时介入了代码:

gain_list = [1.1800000000000068, 0.0, 0.0, 0.10000000000002274, 0.0, 0.44999999999998863, 0.030000000000001137, 0.0, 0.160000000000025, 0.0, 0.0, 0.15000000000000568, 0.0, 0.05000000000001137]
loss_list = [0.0, 0.19999999999998863, 0.10999999999998522, 0.0, 0.0, 0.0, 0.5, 0.3299999999999841, 0.0, 0.47999999999998977, 0.0, 0.10000000000002274, 0.25, 0.0]

avg_gain = 0.15142857142857583
avg_loss = 0.1407142857142836

两者的总和为 2.12,这是我得到的 loss_list 的总和,而我得到的 gain_list 的总和为 2.1799。我错过了什么吗?

如果你看到这里的问题是,无论何时你执行 in_list[-1] 它都不会抛出 IndexError,但它会使用最后一个元素 in_list[len(list)-1]

In [4]: def calculate_avg_gain(in_list : list) -> tuple:
   ...:     gain_list = []
   ...:     loss_list = []
   ...:     for i, close in enumerate(in_list):
   ...:         try:
   ...:             change = close - in_list[i-1]
   ...:         except IndexError:
   ...:             change = 0.0
   ...:         if change >= 0.0:
   ...:             gain_list.append(change)
   ...:             loss_list.append(0.0)
   ...:         else:
   ...:             gain_list.append(0.0)
   ...:             loss_list.append(abs(change))
   ...:         print(gain_list, loss_list, change)
   ...:     avg_gain = sum(gain_list) / 14
   ...:     avg_loss = sum(loss_list) / 14
   ...:     return avg_gain, avg_loss
   ...:

In [5]: calculate_avg_gain(a)
[0.0] [4] -4
[0.0, 1] [4, 0.0] 1
[0.0, 1, 1] [4, 0.0, 0.0] 1
[0.0, 1, 1, 1] [4, 0.0, 0.0, 0.0] 1
[0.0, 1, 1, 1, 1] [4, 0.0, 0.0, 0.0, 0.0] 1
Out[5]: (0.2857142857142857, 0.2857142857142857)

您可以将代码更新为:

In [6]: def calculate_avg_gain(in_list : list) -> tuple:
   ...:     gain_list = []
   ...:     loss_list = []
   ...:     for i, close in enumerate(in_list):
   ...:         if i == 0:
   ...:             continue
   ...:         change = close - in_list[i-1]
   ...:         if change >= 0.0:
   ...:             gain_list.append(change)
   ...:             loss_list.append(0.0)
   ...:         else:
   ...:             gain_list.append(0.0)
   ...:             loss_list.append(abs(change))
   ...:         print(gain_list, loss_list, change)
   ...:     avg_gain = sum(gain_list) / 14
   ...:     avg_loss = sum(loss_list) / 14
   ...:     return avg_gain, avg_loss
   ...:

In [7]: calculate_avg_gain(a)
[1] [0.0] 1
[1, 1] [0.0, 0.0] 1
[1, 1, 1] [0.0, 0.0, 0.0] 1
[1, 1, 1, 1] [0.0, 0.0, 0.0, 0.0] 1
Out[7]: (0.2857142857142857, 0.0)

如果你想找零钱,你可以这样做

>>> [j-i for i, j in zip(t[:-1], t[1:])]
[2, 3]

那么如果变化 > 0,你可以增加收益,如果不是损失。

其他方式:

for i in range(1, len(in_list)):
    change = in_list[i] - in_list[i-1]

如果您已经在您的项目中使用NumPy,您可以

import numpy as np
in_list = np.array([5, 4, 89, 12, 32, 45])
  
  
# Calculating difference list
diff_list = np.diff(in_list)