How can I improve this code?

# max_list = [83, 1350, 1, 100] for i in range(len(max_list)): new_value = 1 while new_value < max_list[i]: new_value *= 10 max_list = new_value 

What am I doing is rounding numbers to the nearest, well, zero value? I do not know what it will be called. But basically, I want 83 → 100, 1 → 1, 1350 → 10000, 100 → 100. I tried to use the round () function, but could not get it to do what I wanted.

This happens, but I thought it could be written in smaller lines.

+4
source share
7 answers

I would do it mathematically:

 from math import ceil, log10 int(pow(10, ceil(log10(abs(x or 0.1))))) 
+11
source
 def nextPowerOfTen(x): if x in [0, 1]: return x elif x < 1: return -nextPowerOfTen(abs(x)) else: return 10**len(str(int(x) - 1)) >>> nextPowerOfTen(83) 100 >>> nextPowerOfTen(1350) 10000 >>> nextPowerOfTen(1) 1 >>> nextPowerOfTen(100) 100 >>> nextPowerOfTen(0) 0 >>> nextPowerOfTen(-1) -1 >>> nextPowerOfTen(-2) -10 

He does something reasonable with negatives, not sure if this is what you want or not.

+3
source

I need it to be 1350/10000 = 0.135, so it is in the range [0, 1].

Why didn’t you say that?

 new_val = float("0." + str(old_val)) 

If you do not need rooms for something else?

+1
source
 >>> x = 12345.678 >>> y = round(x) >>> round(10 * y, -len(str(y))) 100000 
+1
source

pseudo code:

 div = input != 1 ? power(10,truncate(log10(abs(input))) + 1) : 1; percent = input/div; 
0
source

The source code was close and easier to read than some short expression. The problem with your code is a couple of minor errors: initializing new_value each time during the initial scan, and not just once; and replacing max_list with the computed scalar, max_list over it like a list.

In the last line, you should have intended:

  max_list[i] = float(max_list[i]) / new_value 

but you have dropped the index of the array, which will replace the list with one value. In the second iteration of the loop, your Python will throw an exception due to an invalid index to a non-list.

As your code develops more and more new_value values ​​as it moves forward, I recommend that you do not replace list items during the first scan. Do a second scan after calculating the final value for new_value:

 max_list = [83, 1350, 1, 100] # Calculate the required "normalizing" power-of-ten new_value = 1.0 for i in range(len(max_list)): while new_value < max_list[i]: new_value *= 10.0 # Convert the values to fractions in [0.0, 1.0] for i in range(len(max_list)): max_list[i] = max_list[i] / new_value print max_list # "[0.0083000000000000001, 0.13500000000000001, 0.0001, 0.01]" 

Note that I needed to initialize new_value as if it were a floating point value so that it would lead to floating point factors. There are alternative ways to do this, for example, using float(max_list[i]) to get the value to normalize. The initial calculation of new_value began with each element, so your example would new_value == 100 because it was based on the final element in the input list, which is 100.

0
source
 from math import ceil, log10 # works for floats, too. x = [83, 1350, 1, 100, 12.75] y = [10**ceil(log10(el)) for el in x] # alt list-comprehension if integers needed # y = [int(10**ceil(log10(el))) for el in x] 
0
source

Source: https://habr.com/ru/post/1314691/


All Articles