for i in range(0,b):
for j in range(0,a):
ret = net_income[i]/total_capital[j]
roc.append(ret)
これは正しく表示されません。 2つのループでは、最初の純収入を最初の総資本で割った後、最初の純収入を2番目の総資本で割るなど、可能なすべての純利益を可能なすべての資本で割る。しかし、これらの結果のほとんどは破棄されます。 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
に追加されていることがわかります。 roc
に追加された値には、総資本の他の値は使用されません。今
、私の提案された解決策に同じ診断情報を追加:
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
で使用されています
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)
は、より賢明な結果を提供します。
私は何かが不足しているかもしれませんが、最初に設定しますが、 'ret'を上書きしてから' roc'に追加してください。 – steveb