2017-04-04 6 views
0

forループを使用して2つのリストを分割し、最初のリストの最初の要素を2番目のリストの最初の要素に分割します。次に、結果を空のリストに追加しました。以下は、私は新しいリストROCを生成するtotal_capitalでnet_incomeの要素を分けて喜んでコードforループを使用して計算する

def total_capital(df_balance): 
    total_capital = [] 
    for i in range(0,5): 
     cap = df_balance.ix[5,i] 
     total_capital.append(cap) 
    total_capital = [float(x) for x in total_capital] 
    print('Total Capital is :') 
    print(total_capital) 
    print('-----------------------------------') 
    net_income = [] 
    for i in range(0,5): 
     net = df_income.ix[-1,i] 
     net_income.append(net) 
    print('Net Income is:') 
    net_income = [float(x) for x in net_income] 
    print(net_income) 
    a = len(total_capital) 
    b = len(net_income) 
    roc = [] 
    for i in range(0,b): 
     for j in range(0,a): 
      ret = net_income[i]/total_capital[j] 
     roc.append(ret) 
    print('------------------------------------') 

です。しかし、出力結果が間違っています。ここで

は出力です:

Total Capital is : 
[367560073.0, 306315566.0, 279233089.0, 272576179.0, 236272903.0] 
----------------------------------- 
Net Income is: 
[28324711.0, 12887235.0, 6728637.0, 2620339.0, -9393534.0] 
------------------------------------ 
roc is: 
[0.11988133484777981, 0.054543855162265474, 0.028478242382284524, 0.011090306872811396, -0.03975713626373821] 

以上の結果を考えると、私はリストの両方の最初の要素を取得し、それらを手動で分ける場合は、Pythonで関数を呼び出した後、私の答えは、リスト中華民国の最初の要素と一致していません。 。

これは私が必要なものです:

28324711.0/367560073.0 = 0.0770614467692741 

これは私が得たものである:

0.11988133484777981 

は、誰もがこのエラーの原因となっている上記のコードでエラーを指摘することができます。

+0

私は何かが不足しているかもしれませんが、最初に設定しますが、 'ret'を上書きしてから' roc'に追加してください。 – steveb

答えて

3
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.00.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) 

は、より賢明な結果を提供します。

+0

最初の解決策が完全に試されました。エラーを指摘してくれてありがとう。 –

関連する問題