简体   繁体   English

Python中的If语句和设置变量

[英]If Statements and Setting Variables in Python

I'm trying to pass in df1 , df2 , df3 & df4 sequentially into the parseTwoPoleBreakers function. 我正在尝试将df1df2df3df4依次传递到parseTwoPoleBreakers函数中。 However, only df1 is being ran. 但是,仅df1正在运行。 Is there something wrong with my if elif statements that causing df2 , df3 & df4 to not be passed into the parseTwoPoleBreakers function? 我的if elif语句是否有问题,导致df2df3df4无法传递到parseTwoPoleBreakers函数中?

i=0
for j in range(0,4):
    if j==0:
        df=df1
    elif j==1:
        df=df2
    elif j==2:
        df=df3
    else:
        df=df4
    #execute this for each dataframe
    while (i<7):
        parseTwoPoleBreakers(7,8,"ab",i,df)
        breakerid+=1
        parseTwoPoleBreakers(9,10,"bc",i,df)
        breakerid+=1
        parseTwoPoleBreakers(11,12,"ca",i,df)
        breakerid+=1
        i+=1
    #j+=1
    print j

I'm trying to pass in df1, df2, df3 & df4 sequentially into the parseTwoPoleBreakers function 我正在尝试将df1,df2,df3和df4依次传递到parseTwoPoleBreakers函数中

Your problem is most likely related to variable scoping inside the if statements or the fact the while i < 7 isn't entered after df1 because i == 7 您的问题很可能与if语句中的变量作用域有关,或者在df1之后未输入while i < 7的事实,因为i == 7

Use a list instead for the df and did you mean to loop back over the 7 i values? 使用列表代替df ,是不是要循环返回7 i值?

for df in [df1, df2, df3, df4]:
    #execute this for each dataframe
    for i in range(7):
        parseTwoPoleBreakers(7,8,"ab",i,df)
        breakerid+=1
        parseTwoPoleBreakers(9,10,"bc",i,df)
        breakerid+=1
        parseTwoPoleBreakers(11,12,"ca",i,df)
        breakerid+=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM