2017-04-04 92 views
0

我使用一個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('------------------------------------') 

我願意來劃分net_income的元素與total_capital生成一個新的列表ROC。但輸出結果錯誤。

下面是輸出:

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) 

這看起來不正確。通過兩個循環,您將第一個淨收入除以第一個總資本,然後將第一個淨收入除以第二個總資本,以此類推,將每個可能的淨收入除以每個可能的總資本。但是大部分結果都被丟棄了。唯一一次追加到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使用。

+0

嘗試的第一個解決方案完美地工作。感謝您指出錯誤。 –