jeudi 14 février 2019

Python, Making several if-elif-else conditions more efficient

i have one function with 2 arguments. i call that function in other functions. For calling 1st function i use several if-elif-else conditions of arguments. Is it possible to make conditions more efficient using dictionary? code example is below:

def functione_one(arg_1, arg_2):
    ''' SOMETHING RETURNS DF'''
    return df

I use this function above in the several functions. for example one of them:

def other_funct(val1, val2, val3, val4):
    if val1==[]:
        df = functione_one('column-name-1-from-df', val1)
    elif val2==[]:
        df = functione_one('column-name-2-from-df', val2)
    elif val3==[]:
        df = functione_one('column-name-3-from-df', val3)
    else:
        df = functione_one('column-name-4-from-df', val4)

so here everytime i check the condition but instead of writing column names i've used the following line:

def other_funct(val1, val2, val3, val4):
    for i in df.ix[:, [0,1,2,3]]:
'''REST IS SAME BUT INSIDE THIS FOR'''

but i dont know how to check all conditions more efficiently. i wanted to use dictionary:

 values = ['val1', 'val2','val3', 'val4']
 options = {'val1': [], 'val2': [],'val3': [],'val4': []}

But i could not successfully use it so at the end i would like to check all conditions with 'one' line but not writing each of them separately everytime.

Aucun commentaire:

Enregistrer un commentaire