for i in range(0,b):
for j in range(0,a):
ret = net_income[i]/total_capital[j]
roc.append(ret)
這看起來不正確。通過兩個循環,您將第一個淨收入除以第一個總資本,然後將第一個淨收入除以第二個總資本,以此類推,將每個可能的淨收入除以每個可能的總資本。但是大部分結果都被丟棄了。唯一一次追加到ret
的是j
等於a-1。所以你實際上只有最終的總資本。 28324711.0/236272903.0
等於0.11988133484777981
,所以這解釋了你的輸出。
我猜你想通過並行列表進行迭代。嘗試:
for i in range(0,b):
ret = net_income[i]/total_capital[i]
roc.append(ret)
或者可能:
for x,y in zip(net_income, total_capital):
roc.append(x/y)
或者可能:
roc = [x/y for x,y in zip(net_income, total_capital)]
它可能會更容易通過增加診斷的打印資料,瞭解原代碼的行爲:
total_capital = [367560073.0, 306315566.0, 279233089.0, 272576179.0, 236272903.0]
net_income = [28324711.0, 12887235.0, 6728637.0, 2620339.0, -9393534.0]
a = len(total_capital)
b = len(net_income)
roc = []
for i in range(0,b):
for j in range(0,a):
print("calculating ret as the {}th net income divided by the {}th total capital".format(i,j))
ret = net_income[i]/total_capital[j]
print("appending to roc the value of the {}th net income divided by the {}th total capital".format(i,j))
roc.append(ret)
結果:
calculating ret as the 0th net income divided by the 0th total capital
calculating ret as the 0th net income divided by the 1th total capital
calculating ret as the 0th net income divided by the 2th total capital
calculating ret as the 0th net income divided by the 3th total capital
calculating ret as the 0th net income divided by the 4th total capital
appending to roc the value of the 0th net income divided by the 4th total capital
calculating ret as the 1th net income divided by the 0th total capital
calculating ret as the 1th net income divided by the 1th total capital
calculating ret as the 1th net income divided by the 2th total capital
calculating ret as the 1th net income divided by the 3th total capital
calculating ret as the 1th net income divided by the 4th total capital
appending to roc the value of the 1th net income divided by the 4th total capital
calculating ret as the 2th net income divided by the 0th total capital
calculating ret as the 2th net income divided by the 1th total capital
calculating ret as the 2th net income divided by the 2th total capital
calculating ret as the 2th net income divided by the 3th total capital
calculating ret as the 2th net income divided by the 4th total capital
appending to roc the value of the 2th net income divided by the 4th total capital
calculating ret as the 3th net income divided by the 0th total capital
calculating ret as the 3th net income divided by the 1th total capital
calculating ret as the 3th net income divided by the 2th total capital
calculating ret as the 3th net income divided by the 3th total capital
calculating ret as the 3th net income divided by the 4th total capital
appending to roc the value of the 3th net income divided by the 4th total capital
calculating ret as the 4th net income divided by the 0th total capital
calculating ret as the 4th net income divided by the 1th total capital
calculating ret as the 4th net income divided by the 2th total capital
calculating ret as the 4th net income divided by the 3th total capital
calculating ret as the 4th net income divided by the 4th total capital
appending to roc the value of the 4th net income divided by the 4th total capital
你可以看到,唯一的一次什麼是不斷追加到roc
,它使用4總資本。 roc
附加值中沒有其他總資本值。
現在,添加相同的診斷信息,我建議解決方案:
total_capital = [367560073.0, 306315566.0, 279233089.0, 272576179.0, 236272903.0]
net_income = [28324711.0, 12887235.0, 6728637.0, 2620339.0, -9393534.0]
a = len(total_capital)
b = len(net_income)
roc = []
for i in range(0,b):
print("calculating ret as the {}th net income divided by the {}th total capital".format(i,i))
ret = net_income[i]/total_capital[i]
print("appending to roc the value of the {}th net income divided by the {}th total capital".format(i,i))
roc.append(ret)
給出了一個較爲合理的結果:
calculating ret as the 0th net income divided by the 0th total capital
appending to roc the value of the 0th net income divided by the 0th total capital
calculating ret as the 1th net income divided by the 1th total capital
appending to roc the value of the 1th net income divided by the 1th total capital
calculating ret as the 2th net income divided by the 2th total capital
appending to roc the value of the 2th net income divided by the 2th total capital
calculating ret as the 3th net income divided by the 3th total capital
appending to roc the value of the 3th net income divided by the 3th total capital
calculating ret as the 4th net income divided by the 4th total capital
appending to roc the value of the 4th net income divided by the 4th total capital
現在從總股本中的所有值roc
使用。
我可能失去了一些東西,但首先你確定,但你一直覆蓋'ret'然後將其添加到'roc' – steveb