0

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.

Ave
  • 39
  • 5

3 Answers3

1

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]
re-za
  • 750
  • 4
  • 10
  • Thank you so much sir! That's exactly what I need! – Ave Mar 24 '22 at 13:27
  • 1
    @user18560399 This was completely different from your original question. Please try to write clear questions. This should have been a new question. – Chris_Rands Mar 25 '22 at 19:34
0

Slice the list, apply round(), re-assign the list:

>>> a[:3] = map(round, a[:3])
>>> a
[100, 122, 130, 140.5, 150.5, 160.5, 170.5, 180.5, 190.5, 200.5]

Note, I'm assuming you want to round the values and not make them all 100. Otherwise, you can use a function like this:

>>> from math import log10, floor
>>> def round_to_1(x):
...     return round(x, -int(floor(log10(abs(x)))))
... 
>>> a[:3] = list(map(round_to_1, a[:3]))
>>> a
[100.0, 100.0, 100.0, 140.5, 150.5, 160.5, 170.5, 180.5, 190.5, 200.5]
Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
  • For some reason they want to round the first three elements down to 100 apparently . Look at the expected output! (unless it's a typo) – re-za Mar 23 '22 at 19:04
  • Thats good, thanks but what if these numbers are in random order [100.50, 200.50, 121.50, 140.50, 130.50, 150.50, 160.50, 180.50, 190.50, 170.50] I still need them to to round down to 100.0 if they are < 130.0 And not by index cause I want same with 200.0 till 230.0, 300. till 330 etc – Ave Mar 23 '22 at 20:27
  • @user18560399 that's a completely different question! You should update your question – re-za Mar 23 '22 at 20:49
  • @user18560399 see my answer. But consider updating your question for other people – re-za Mar 23 '22 at 21:13
-1
>>> def round_first_n(arr, n):
...     for i in range(n):
...             arr[i] = round(arr[i])
...
>>> arr = [100.5]*10
>>> arr
[100.5, 100.5, 100.5, 100.5, 100.5, 100.5, 100.5, 100.5, 100.5, 100.5]
>>> round_first_n(arr, 3)
>>> arr
[100, 100, 100, 100.5, 100.5, 100.5, 100.5, 100.5, 100.5, 100.5]

To round at arbitrary indices:

>>> def round_at(arr, indices):
...     for i in indices:
...             arr[i] = round(arr[i])
...
>>> round_at(arr, [5,7])
>>> arr
[100, 100, 100, 100.5, 100.5, 100, 100.5, 100, 100.5, 100.5]
lmeninato
  • 462
  • 4
  • 12
  • You might want to add bounds checking ;) (since if n > len(arr) -1) you will get an "out of bounds" error, since you'd be accessing an index of the array that does not exist) – lmeninato Mar 23 '22 at 18:55