dimanche 5 mai 2019

How to translate nested if-else in python list comprehension

I am trying to convert the "conventional" nested if-else to list comprehension in python but I was not able to accomplish it.

def functst(a, list_a, b, list_b):
    list_result = []
    for i in range(len(list_a)):
        if a != 0:
            if list_a[i] <= a:
                list_result.insert(i, 1)
            else:
                list_result.insert(i, a/list_a[i] * (1 - list_b[i]))
        elif list_a[i] <= b:
             list_result.insert(i,1)
        else:
            list_result.insert(i, b/list_a[i] * (1-list_b[i]))
    return list_result

I would like to convert it to list comprehension using nested if-else. Any inputs?

Aucun commentaire:

Enregistrer un commentaire