我使用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中调用函数后给出上面的结果如果我取两个列表的第一个元素并手动划分它们我的答案与列表roc的第一个元素不匹配。
这就是我想要的:
28324711.0 / 367560073.0 = 0.0770614467692741
这就是我得到的:
0.11988133484777981
任何人都可以指出导致此错误的上述代码中的错误。
答案 0 :(得分: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
中使用。