dimanche 14 mars 2021

if/else and for in 1 line python? [duplicate]

Here is a little problematic. It's easy to write but it's taking too much place for something that we can understand easely even if it's in only one line of code.

ACTUAL:

value_list = [0.2,0.3,0.7,1,0.23]

def get_thresholded_list(value_list, treshold=0.5):
    threshold_list = []
    for v in value_list:
        if v < treshold: v=0
        else: v=1
        threshold_list.append(v)
    return threshold_list

print(get_thresholded_list(value_list))

I want to change my function to have something like this:

value_list = [0.2,0.3,0.7,1,0.23]

def get_thresholded_list(value_list, treshold=0.5):
    return [...]

print(get_thresholded_list(value_list))

Aucun commentaire:

Enregistrer un commentaire