I have a list in Python
a = [100.50, 121.50, 130.50, 140.50, 150.50, 160.50, 170.50, 180.50, 190.50, 200.50]
I want to round first three values
[100.00, 100.00, 100.00, 140.50, 150.50, 160.50, 170.50, 180.50, 190.50, 200.50]
I need to round down to 100.0 if they are < 130.0 and then similarly round down to 200 if they are < 230 and so on.
Advertisement
Answer
Based on comments it seems you want to round down to 100.0 if they are < 130.0 and then similarly round down to 200 if they are < 230 and so on.
So the following:
b = [100*(x//100) if x % 100 < 30 else x for x in a]
So for every element x
of list a
, if the remainder of dividing it by 100 is less than 30 we have to round it down other wise keep x
.
For rounding down to 100, 200, … integer divide x
by 100 and then multiply again by 100.
Example
a = [100.50, 121.50, 130.50, 220.50, 240.50, 310.50, 350.50]
Output
[100.0, 100.0, 130.5, 200.0, 240.5, 300.0, 350.5]