Python 使用for循环进行计算

Python 使用for循环进行计算,python,for-loop,Python,For Loop,我使用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

我使用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('------------------------------------')
我愿意将净收入要素除以总资本,以生成新的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
有人能指出上面代码中导致此错误的错误吗

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.1198813348477981
,因此这解释了您的输出

我猜您希望并行地遍历列表。尝试:

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)]
或者可能:

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
的值

现在,将相同的诊断信息添加到我建议的解决方案中:

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
的唯一时间是
j
等于a-1时。所以你实际上只是除以最终总资本
28324711.0/236272903.0
等于
0.1198813348477981
,因此这解释了您的输出

我猜您希望并行地遍历列表。尝试:

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)]
或者可能:

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
的值

现在,将相同的诊断信息添加到我建议的解决方案中:

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

现在,total capital中的所有值都用在
roc

中,我可能遗漏了什么,但首先您设置了,但您一直覆盖
ret
,然后将其添加到
roc
中,我可能遗漏了什么,但首先您设置了,但您一直覆盖
ret
,然后将其添加到
roc
尝试第一个解决方案,效果非常好。感谢您指出错误。尝试了第一个解决方案,效果非常好。谢谢你指出错误。