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.
source share