vendredi 31 mars 2017

Recoding floats in a list

I have a list of floats, which looks like this:

ratings_dec = [13.0, 8.6, 4.9, -1.5, 6.2, 7.7, 2.0, 10.0, 7.7, 12.7]

I want to clean this data, by giving number higher than 10.0 a 10.0 and numbers lower than 0.0 (so all negative numbers) a 10.0. I'm doing this with the following if statement:

predictions_clean = []
for pred in predictions_dec:
    if pred >= 10:
        predictions_clean.append(10.0)
    if pred <= 0:
        predictions_clean.append(0.0)
    else:
        predictions_clean.append(pred)

This code seems to work, but the funny thing is that:

len(predictions_dec) 
1222
len(predictions_clean)
1816

My understanding of if statements is not that great. Where in the if statement am I doing something wrong?

Aucun commentaire:

Enregistrer un commentaire