Doubt the use of If, While or for

Asked

Viewed 77 times

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 eliminate (remove) values that are below 30 and above 160, and create a new vector with these values.

  • 1

    And what exactly is the doubt?

  • 1

    Publish the code you tried and failed to, for better help

  • Something like that if ($item > 30 && $item < 160) dai Voce can play the dice inside another array if it passes validation

1 answer

3

Juarez, the best way to do this type of filtering is through the technique of list comprehension:

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 it was not clear.

With its 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 erase the values from the list, because that would not be smart, but rather creates another one with the correct values. I hope I helped :D!

  • Yan has two variables M1 and m2 and a vector (measured) with values, need to see if the values of the vector are greater than M1 or smaller than m2 and exclude them if they are. And list to see which values were excluded.

  • Juarez, I understand, but my explanation was not clear? : (. I will edit the answer to be more specific to your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.