Doubt the use of If, While or for

0

I'm new to python programming. I have a vector with values ranging from 0 to 200 randomly and I want to do a routine to delete (remove) values that are below 30 and above 160, and create a new vector with those values.     

asked by anonymous 30.04.2018 / 21:39

1 answer

3

Juarez, the best way to do this type of filtering is through the technique of understanding lists :

vetor_filtrado = [valor for valor in vetor if 30 <= valor <= 160]

This assumes that the vector you are referring to is actually called vetor , if not clear.

To your specifications:

excluidos = [valor for valor in vetor if valor < 30 or valor > 160
vetor = [valor for valor in vetor if 30 <= valor <= 160]

The code does not actually delete the values from the list, because this would not be smart, but rather creates another one with the correct values. I hope I have helped: D!

    
30.04.2018 / 22:38