Your original code was close, and more easily read than some terse expression. The problem with your code is a couple of minor errors: initializing new_value
each time in the initial scan, rather than only once; and replacing the max_list
with a calculated scalar while looping over it as a list.
On the final line, you must have intended:
max_list[i] = float(max_list[i]) / new_value
but you dropped the array index, which would replace the list with a single value. On the second iteration of the loop, your Python would give an exception due to the invalid index into a non-list.
Because your code develops greater and greater values of new_value as it progresses, I recommend you not replace the list items during the first scan. Make a second scan once you calculate a 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]"
Notice that I was required to initialize new_value as if it were a floating-point value, in order that it would result in floating-point quotients. There are alternative ways to do this, such as using float(max_list[i])
to retrieve the value for normalizing. The original calculation of new_value
was starting over with each element, so your example would return new_value == 100
because this was based off the final element in the input list, which is 100.